0

コードを2〜3クラスに変更/分解したり、コンストラクターを追加したり(可能であれば空ではない)、メソッドを追加したりできるかどうかを尋ねたいと思います。必要に応じて、プログラムはより多くの機能を持つことができます。

 public class Testing {


            public static void main(String args[]) throws Exception {
                Scanner input = new Scanner(System.in);
                System.out.println("Select word from list:");
                System.out.println();

                try {
                    FileReader fr = new FileReader("src/lt/kvk/i3_2/test/List.txt"); // this is list of words, everything all right here
                    BufferedReader br = new BufferedReader(fr);
                    String s;
                    while((s = br.readLine()) != null) {
                        System.out.println(s);
                    }
                    fr.close();
                    String stilius = input.nextLine();   // eneter word which I want to count in File.txt
                    BufferedReader bf = new BufferedReader(new FileReader("src/lt/kvk/i3_2/test/File.txt")); // from this file I need to count word which I entered before

                    int counter = 0;                
                    String line;

                    System.out.println("Looking for information");
                    ArrayList<String> resultList = new ArrayList<String>();
                    String name = null;
            while (( line = bf.readLine()) != null){
                    if (line.trim().length() == 0) name = null;
                    else if (name == null) name = line;
                    int indexfound = line.indexOf(stilius);
                          if (indexfound > -1) {
                   counter++;
                   resultList.add(name);
   }
                    }
                    if (counter > 0) {
                        System.out.println("Word are repeated "+ counter + "times");}
                        else {
                        System.out.println("Error...");
                    }
                    bf.close(); 

                }
                catch (IOException e) {
                    System.out.println("Error:" + e.toString());
                    }
                }
            }

file.txtから(キーボードで入力された)単語を数えるプログラムを作成し、この単語を繰り返した人を選択します。例:単語を入力した場合:次のようOneに表示されます。

Word One repeated 3 times by John, Elisa, Albert

file.txtは次のようになります。

John //first line - name
One
Three
Four

Peter //first line - name
Two
Three

Elisa //first line - name
One
Three

Albert //first line - name
One
Three
Four

Nicole //first line - name
Two
Four

このコードを2〜3クラスに分解できるかどうかはわかりません。誰か助けてくれたらどうもありがとう。

4

1 に答える 1

0

まず、2つのクラスを定義します。

  • WordFile
  • WordFileEntry

WordFile-objectは、WordFileEntry-objectsのリストで構成されている必要があります。WordFileEntryは、文字列名とList<String>単語で構成されます。

繰り返しのカウントは、WordFileオブジェクト自体によって実行できます。ファイルを読み取るロジックは、WordFileクラスまたは別のクラスで記述できます。

于 2012-11-12T12:43:01.663 に答える