KIWC implementation based on the main program/subroutine style

Source: Internet
Author: User

/** *  */package com.jason.kwic.mainsubroutine;import java.io.file;import  java.io.ioexception;import java.util.arraylist;import java.util.collections;import  java.util.comparator;import java.util.scanner;/** *  KIWC Implementation  *  @author based on the main program/subroutine style  jasonzhang * */public class mainsubroutine {private arraylist<arraylist <String>> linelist = new ArrayList<ArrayList<String>> ();p rivate  Arraylist<string> shiftedlinelist = new arraylist<string> ();p ublic static  void main (String[] args)  throws ioexception {mainsubroutine main =  new mainsubroutine (); Main.input (); Main.shift (); Main.alphabetizer (); Main.output ();} Private void input ()  throws ioexception {file infile = new file (" D:\\temp\\mykwic_in.txt "); scanner sc =  New scanner (infile); string templine = null; System.out.println ("Input is");while  (Sc.hasnextline ())  {templine = sc.nextline (); System.out.println (Templine); This.linelist.add (This.linesplitword (Templine));}} Private void shift ()  {for  (arraylist<string> wordline : this.linelist )  {this.shiftwordline (Wordline);}} Private void alphabetizer ()  {//sorted by Alphabet          Collections.sort (This.shiftedlinelist, new alphaabetizercomparator ());} Private void output ()  {system.out.println ("Output is");for  (String line :  this.shiftedlinelist)  {system.out.println (line);}} Private arraylist<string> linesplitword (String line)  {ArrayList<String>  Wordlist = new arraylist<string> (); string word =  "";         int&nbSp;i = 0;        while (I < line.length ()) {             if (Line.charat (i)  !=  '   ') {                word + = line.charat (i);            }             else{             wordlist.add (word);                 word =  "";             }            i++;         }        if  (Word.length ()  > 0) &NBSP;{&NBSp;       wordlist.add (word);         }return wordlist;} Private void shiftwordline (Arraylist<string> wordline)  {stringbuilder templine  = new stringbuilder (); Int wordnums = wordline.size ();//System.out.println (" Shifed is ");for  (int i=0; i<wordnums; i++)  {for  (int j= (wordNums  - 1 -i);  j < wordnums; j++)  {templine.append (WordLine.get (j)). Append ('   ');} for  (int k=0; k< (wordnums - 1 -i);  k++)  {if  (k !=  ( WORDNUMS&NBSP;-&NBSP;I&NBSP;-&NBSP;2))  {templine.append (Wordline.get (k)). Append ('   ');}  else {templine.append (Wordline.get (k));}} System.out.println (Templine.tostring ()); This.shiftedLineList.add (templine.tostring ()); Templine.delete (0,  templine.length ());}} Private class alphaabetizercomparator implements comparator<string> {@Overridepublic  int compare ( STRING&NBSP;O1,&NBSP;STRING&NBSP;O2)  {if  (o1 == null | |  o2 == null)  {             Throw new nullpointerexception ();        }int  Comparevalue = 0;char o1firstcharacter = o1.charat (0); char o2FirstCharacter  = o2.charat (0); if (This.isletter (o1firstcharacter)  && this.isletter ( O2firstcharacter))  {//If the value of the lowercase letter is converted to the value of the corresponding uppercase letter o1firstcharacter = this.touppercase ( O1firstcharacter); O2firstcharacter = this.touppercase (O2firstcharacter);compareValue =  O1firstcharacter - o2firstcharacter;}  else {throw new runtimeexception ("must be a letter");} Return comparevalue;} Private boolean isletter (char c) &NBSP;{RETURN&NBSP; (c >= 65 && c <= 90)  | |   (c >= 97 && c <= 122);} Private char touppercase (char c)  {if  (Character.islowercase (c))  {return  Character.touppercase (c);} Return c;}         }}

This style is basically a class that implements all things.

KIWC implementation based on the main program/subroutine style

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.