FileNotFoundException在输入提示符后输入字符串

时间:2018-04-22 17:30:42

标签: java

我不确定我是不是以正确的方式输入我的代码,或者我的实际代码中的错误是在哪里。我对#34;尝试"相对较新。 "捕捉"当我在Java中运行我的代码覆盖时,它显示在我输入输入的字符串后,它直接转到错误。对于此代码的目的,它们不仅仅是一个类,但代码在错误发生之前并未运行所有类。代码的目的是输入有关学生的信息,并通过代码确定它们是否匹配在一起。这个类特别是该程序的主要类。当我输入像#34; Abey,"而且我会收到错误。

错误: 请给出学生姓名: Abey java.io.FileNotFoundException:Abey(系统找不到指定的文件)

我的代码

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.util.NoSuchElementException;

import java.util.Scanner;



public class Match {



 public static void main(String[] args) {



  Student[] arr = new Student[100];



  System.out.println("Please give the student name: ");

  Scanner input = new Scanner(System.in);

  String filename = input.next();


  Scanner nameInput;
  try {
   nameInput = new Scanner(new FileReader(filename));


   int i = 0;



   while (nameInput.hasNextLine()) {



    Scanner ab = new Scanner(nameInput.nextLine());

    ab.useDelimiter("[\t-]");



    String name = ab.next();

    String gender = ab.next();

    String date = ab.next();



    Scanner birthDateReader = new Scanner(date);

    birthDateReader.useDelimiter("-");

    int month = birthDateReader.nextInt();

    int day = birthDateReader.nextInt();

    int year = birthDateReader.nextInt();



    int quietTime = ab.nextInt();

    int music = ab.nextInt();

    int reading = ab.nextInt();

    int chatting = ab.nextInt();



    Date birthdate = new Date(month, day, year);

    Preference pref = new Preference(quietTime, music, reading, chatting);

    Student studentAdd = new Student(name, gender.charAt(0), birthdate, pref);

    arr[i++] = studentAdd;



   }



   int max = i;

   for (i = 0; i < max; i++) {

    if (!arr[i].getMatch()) {

     int bestScore = 0;
     int bestMatch = 0;

     for (int j = i + 1; j < max; j++) {

      if (!arr[j].getMatch()) {

       int tmp = arr[i].compare(arr[j]);

       if (tmp > bestScore) {

        bestScore = tmp;

        bestMatch = j;





       }

      }

     }

     if (bestScore != 0) {

      arr[i].setMatched(true);

      arr[bestMatch].setMatched(true);

      System.out.println(arr[i].getName() + " can match with " + arr[bestMatch].getName() + " with the score " + bestScore);

     } else

     if (!arr[i].getMatch())

      System.out.println(arr[i].getName() + " Does not have any matches.");

    }

   }

   input.close();
  } catch (NoSuchElementException e) {
   System.out.println(e);

  } catch (FileNotFoundException e) {
   System.out.println(e);
  }

 }

}

2 个答案:

答案 0 :(得分:0)

该进程无法找到相对于工作目录的Abey文件。尝试指定完整路径:

File root = new File("/path/to/data/files");
...
String filename = ....;
File datafile = new File(root, filename);
try (FileReader reader = new FileReader(datafile)) {
  ....
}

答案 1 :(得分:-1)

主要问题是,程序正在搜索相对路径。您需要提供文件的完整路径。

String completePath = "/opt/java/path/"
Scanner input = new Scanner(System.in);

String filename = input.next();


Scanner nameInput;
try {
nameInput = new Scanner (new FileReader(completePath+filename));

这将是您修改后的代码。

此处 completePath 变量包含按学生名称存储文件的文件夹的路径。