我是编码的新手,由于某些原因,我无法将文件路径复制到Java中。我一直在尝试复制和粘贴文件路径以及绑定文件路径,无论我做什么,Java博士都找不到该文件。我一直在尝试找到解决方案,但是找不到可行的解决方案。我尝试使用[this]:How to provide file path in Mac OS X while creating a file in java?作为参考/解决方案,但它对我不起作用。这是我遇到问题的代码。
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class GradeExam6{
public static void main(String [] args) throws FileNotFoundException{ // gives the file not found exception when a file is enteres that does not exist or that cannot be located
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the file."); // gets the file from the user
String fileName = sc.nextLine(); // stores the file that the user wants read
File file = new File(fileName);
Scanner reader = new Scanner (file);
String line = reader.nextLine();
String answer = null;
//arrays
String [][] student = new String[100][2];
int [] results = new int[100];
int b=0;
while(line!= null){
if(b == 0)
answer = line;
else{
student[b-1]=line.split(" ",2);
}
b++;
}
for(int a=0; a < b-1; a++){
int score = 0;
for(int c=0; c<answer.length(); c++){
if(answer.charAt(a) == student[b][1].charAt(a)){
score+=2; // adds 2 points to the score if the answer is correct
}
else if(student[b][1].charAt(a) == ' '){ // does not count or take off from the score because the answer is blank
}
else{
score-=1; // takes off 1 point if the answer is wrong
}
}
results[b]=score; // gets the total score
}
for(int a=0; a < b-1; a++){
System.out.println("Student ID Number: " +student[b][0]); // gives the student's id number
System.out.println("Student Answers: " +student[b][1]); // gives the student's test answers
System.out.println("Score: " +results[b]); // gives the student's test score
sc.close();
reader.close();// closes the scanners
}
}
}
我正在尝试使用一个简单的.txt文件。