2017-12-13 15 views
-2

ファイルと各行を最終行と比較してファイルに送信することで、回答キーを読み取ろうとしています。私はファイルに送信しようとしましたが、出力がありません、問題は何ですか?
は、ここにコードファイルに出力されないjava

​​

輸入java.io. *を入力してください。 import java.io.PrintWriter;あなたはTXTがあなたのファイルたとえばファイルの内容を読みたい場合は

パブリッククラスtester3 { のpublic static無効メイン(文字列[] args)はIOExceptionが{

final int answer_size = 20; 
    final int class_size = 20; 

    final int answer_key_size = 20; 

    String [][] answers = new String[answer_size][class_size]; 
    String [] correctAns = new String[20]; 

    readArray(answers); 
    getAnswers(answers, correctAns); 
    compareAnswers(answers, correctAns); 


} 

//read file ans into char array 
public static void readArray(String[][] ar)throws IOException{ 
    File file = new File("answers.txt"); 
    Scanner inputFile = new Scanner(file); 
    int index = 0; 
    while(inputFile.hasNextInt() && index < ar.length){ 
    for(int row = 0; row < ar.length; row++){ 
     for(int col = 0; col < ar[row].length; col++){ 
      ar[row][col] = inputFile.nextLine(); 
      index++; 
      //ar[row][col] = Character.toString(ar[row][col].charAt(row)); 
      System.out.println(ar[row][col]); 
     } 
    } 
} 

} 

//read last row into 1d array 
public static void getAnswers(String[][] ar, String [] ar2)throws IOException{ 
      File file = new File("answers.txt"); 
      Scanner inputFile = new Scanner(file); 
      int index = 0; 
      while(inputFile.hasNextInt() && index < ar.length){ 
      for(int row = 0; row < ar.length; row++){ 
       for(int col = 0; col < ar[row].length; col++){ 
        //ar[row][col] = inputFile.nextLine().charAt(row); 
        if(row == 20){ 
         inputFile.nextLine(); 
         ar2[row] = Character.toString(ar[row][col].charAt(row)); 
        } 
       } 
       index++; 
      } 
} 

} 
//compare answers and send the results to an output file 
public static void compareAnswers(String[][] ar, String [] ar2)throws IOException{ 
    PrintWriter writer = new PrintWriter("results.txt"); 
    File file = new File("answers.txt"); 
    int [] grades = new int[20]; 
        Scanner inputFile = new Scanner(file); 
        int index = 0; 
        int count = 0; 

        writer.println("STUDENT EXAM RESULTS"); 
        writer.println("Student number ---- # correct answers ---- Grade Received"); 

        while(inputFile.hasNextInt() && index < ar.length){ 
        for(int row = 0; row < ar.length; row++){ 
         for(int col = 0; col < ar[row].length; col++){ 
          ar[row][col] = Character.toString(ar[row][col].charAt(row)); 
          if(ar[row][col].equals(ar2[row])){ 
           count++; 
           grades[row] = (count/20) * 100; 
           writer.println(ar[row][col] + "----" + count + "----" + grades[row]); 
           writer.flush(); 
          } 
         } 
         count = 0; 
         index++; 
        } 
} 

    writer.close(); 

} 

}

+0

@カラン:なぜあなたはそのような声明をしますか?いつScannerオブジェクトでファイルを解析できないのですか? –

答えて

0

をスローしますBufferedReaderを使用してください。 filePathあなたが読んで、取り扱いが容易で、文字列のArrayListとしてファイルの内容を返したいファイルの絶対パスにすることができ

public ArrayList<String> getFileLines(String filePath) throws IOException { 
    ArrayList<String> content = new ArrayList<>(); 
    String line = ""; 
    BufferedReader in = new BufferedReader(new FileReader(filePath)); 
    while((line=in.readLine()) != null) { 
     content.add(line); 
    } 
    return content; 
} 

:あなたはこのような何かを行うことができます。

次に、TXTファイルなどのファイルに何かを書きたい場合は、BufferedWriterを使用してください。この場合filePath

public void writeArrayInFile(String filePath, ArrayList<String> content) throws IOException { 
    BufferedWriter out = new BufferedWriter(new FileWriter(filePath)); 
    for (String l : content) { 
     out.write(l + "\n"); 
    } 
    out.close(); 
} 

あなたはファイルが既にこの方法は、ファイルの内容が上書きされますが存在する場合は作成しますが、慎重になりたいファイルの絶対パスでなければなりません。そして、ArrayList<String>はあなたが書きたいコンテンツです。

関連する問題