ファイルと各行を最終行と比較してファイルに送信することで、回答キーを読み取ろうとしています。私はファイルに送信しようとしましたが、出力がありません、問題は何ですか?
は、ここにコードファイルに出力されない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();
}
}
@カラン:なぜあなたはそのような声明をしますか?いつScannerオブジェクトでファイルを解析できないのですか? –