[Java exercise] kwic Simulation

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Package org. BUPT. kwic;
Import java. Io. bufferedreader;
Import java. Io. filereader;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. collections;
Import java. util. stringtokenizer;
Public class mykwic {
Private Static bufferedreader input_file;
Private arraylist <string> kwiclist;
Public mykwic (string filename) // construct the index of file fname
{
Kwiclist = new arraylist <string> ();
String line = "";
Fileopen (filename );
While (line! = NULL)
{
Line = Readline ();
If (line! = NULL)
{
Parseline (line, kwiclist );
}
}
// Collections. Sort (kwiclist );
Display (kwiclist );
}
Public static void fileopen (string inputfilename ){
Try {
Input_file = new bufferedreader (New filereader (inputfilename ));
} Catch (ioexception e ){
System. Err. println ("file not open" + E. tostring ()));
System. Exit (1 );
}
}
Public static string Readline (){
String line = "";
Try {
Line = input_file.readline ();
} Catch (exception e ){
E. getstacktrace ();
}
Return line;
}
Public void parseline (string line, arraylist <string> List ){
Stringtokenizer tokener = new stringtokenizer (line );
String token = new string ();
Int index;
Arraylist <string> tokens = new arraylist <string> ();
Int COUNT = tokener. counttokens ();
For (Int J = 0; j <count; j ++) {// parse a row and add the resolved word to the arraylist
Token = tokener. nexttoken ();
Tokens. Add (token );
}
// Shift the words in the arraylist cyclically to obtain the final result
For (INT I = 0; I <count; I ++ ){
Index = I;
Stringbuffer linebuffer = new stringbuffer ();
For (Int J = 0; j <count; j ++ ){
If (index> = count)
Index = 0;
Linebuffer. append (tokens. Get (INDEX ));
Linebuffer. append ("");
Index ++;
}
Line = linebuffer. tostring ();
Kwiclist. Add (line );
}
}
Public static void display (arraylist <string> List ){
System. Out. println ("output is ");
For (int count = 0; count <list. Size (); count ++ ){
System. Out. println (list. Get (count ));
}
}
Public static void main (string [] ARGs ){
New mykwic ("test.txt ");
}
}

 

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.