2016-03-30 14 views
0

私はクラスの割り当てに取り組んでいます。私は、入力行を含むファイルを読み込む必要があります。 3行の各ブロックは構造化されたセットです。最初は1つの多項式、2つ目は別の多項式、3つ目は多項式演算の代数演算を示すテキスト文字列です。プログラムを設定して、各行を配列に読み込んだ後、整数を含む2つの配列インデックスを多項式の項に解析します。 3行目に基づいて適切な関数を呼び出します。私の苦労は、3行目ごとにプロセスをリセットする方法を見つけることです。ここに私の主な機能のコードです。私は何とかiループ(ここではkループ)を使用すると思っていましたが、動作させることはできません。任意の洞察力や提案を大いに感謝します。入力の入力のセグメントに基づいて関数を循環的に呼び出す

例:ここでは

3 2 4 5 

5 7 4 6 

subtract 

4 3 5 1 

1 2 3 4 

add 

が私のコードです:

public static void main(String[] args) throws IOException { 

    Polynomial p1 = new Polynomial(); 
    Polynomial p2 = new Polynomial(); 
    int lines = 0; 

    List<String> list = new ArrayList<String>(); 

    try { 
     BufferedReader reader = new BufferedReader(new FileReader("Test.txt")); 

     String line=null; 

     while((line = reader.readLine()) != null) { 
      list.add(line); 
      lines++; 
     } // end while 
    } catch (FileNotFoundException e){ 
     e.printStackTrace(); 
    } 
    System.out.println("lines " + lines); 

    for (int k=0; k<lines; k++){ 
     String[] stringArr = list.toArray(new String[0]); 
     System.out.println(stringArr[k+0]); 
     System.out.println(stringArr[k+1]); 
     System.out.println(stringArr[k+2]); 

     String[] nums1 = stringArr[k+0].split(" "); 
     String[] nums2 = stringArr[k+1].split(" "); 

     for (int i=0; i<nums1.length; i+= 2) { 
      p1.addTerm(Integer.parseInt(nums1[i]), Integer.parseInt(nums1[i+1])); 
     } 
     for (int i=0; i<nums2.length; i+= 2) { 
      p2.addTerm(Integer.parseInt(nums2[i]), Integer.parseInt(nums2[i+1])); 
     } 

     if (stringArr[k+2].equalsIgnoreCase("add")) {add(p1,p2);} 
     else if (stringArr[k+2].equalsIgnoreCase("subtract")) {subtract(p1,p2);} 
     else if(stringArr[k+2].equalsIgnoreCase("multiply")) {multiply(p1,p2);} 
     else { 
      System.out.println("Bad input"); 
     } 
     nums1=null; 
     nums2=null; 
    } 
} 

答えて

0

提案:Scannerクラスのようなものを試してみてください?ファイル内に行がなくなる場合は、NoSuchElementExceptionをキャッチしてください。

Scanner scanner = new Scanner(new String("input")); 

while(scanner.hasNextLine()) { 
     String lineOne = scanner.nextLine(); 
     String lineTwo = scanner.nextLine(); 
     String lineThree = scanner.nextLine(); 
     calculateSomething(lineOne, lineTwo, lineThree); 
    } 

それは良い夜の睡眠での文字列(デフォルトではスペース区切り)

private static int[] getPolynomialFactors(String line) { 
    Scanner splitter = new Scanner(line); 

    int[] p = new int[4]; 
    int counter=0; 

    while (splitter.hasNextInt()){ 
     p[counter] = splitter.nextInt(); 
     counter++; 
    } 
    return p; 
} 
+0

ありがとうございました!私はファイルからの入力を読むことを学んでいるので、Scannerに関するヒントは非常に役立ちます。 –

0

を読み取るために使用することができ、私はそれを把握することができました。私はfor-loopを使用し、ループカウンタのインクリメントを変更するだけでした。私はコードブロックの最初の2行から解析された整数を格納するために使用していた整数配列へのポインタを無効にしました。更新されたコードがあります:

 public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 

     Polynomial p1 = new Polynomial(); 
     Polynomial p2 = new Polynomial(); 
     int lines = 0; 

     List<String> list = new ArrayList<String>(); 

     try { 
      BufferedReader reader = new BufferedReader(new FileReader("Test.txt")); 

      String line=null; 

      while((line = reader.readLine()) != null) { 
       list.add(line); 
       lines++; 
      } // end while 
     } catch (FileNotFoundException e){ 
      e.printStackTrace(); 
     } 
     System.out.println("lines " + lines); 
     String[] stringArr = list.toArray(new String[0]); 


     for (int k=0; k<lines; k+=3){ 

      System.out.println(stringArr[k+0]); 
      System.out.println(stringArr[k+1]); 
      System.out.println(stringArr[k+2]);    


      String[] nums1 = stringArr[k+0].split(" "); 
      String[] nums2 = stringArr[k+1].split(" "); 

      for (int i=0; i<nums1.length; i+= 2) { 
       p1.addTerm(Integer.parseInt(nums1[i]), Integer.parseInt(nums1[i+1])); 
      } 
      for (int i=0; i<nums2.length; i+= 2) { 
       p2.addTerm(Integer.parseInt(nums2[i]), Integer.parseInt(nums2[i+1])); 
      } 

      if (stringArr[k+2].equalsIgnoreCase("add")) {add(p1,p2);} 
      else if (stringArr[k+2].equalsIgnoreCase("subtract")) {subtract(p1,p2);} 
      else if(stringArr[k+2].equalsIgnoreCase("multiply")) {multiply(p1,p2);} 
      else { 
       System.out.println("Bad input"); 
       break; 
      } 
      nums1=null; 
      nums2=null; 
     } 
    } 
} 
関連する問題