Файлы на MacBook Pro с Dr Java - PullRequest
       12

Файлы на MacBook Pro с Dr Java

0 голосов
/ 03 декабря 2018

Я новичок в кодировании и по некоторым причинам не могу скопировать путь к файлу в Java.Я пытался скопировать и вставить путь к файлу, а также связать путь к файлу, и независимо от того, что я делаю, доктор Ява не может найти файл.Я пытался найти решение для этого, но я не могу найти решение, которое работает.Я попытался использовать [this]: Как указать путь к файлу в Mac OS X при создании файла в 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.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...