-2

I'm not sure if I'm not entering my code the right way, or where the error in my actual code is. I'm relatively new to "try" "catch" and when I run the coverage of my code in Java it shows that after I enter the inputted string it goes straight to the error. Their is more than one class for this code's purpose but the code doesn't run through all of the classes before the error. The purpose of the code is to enter information about students and through the code determine if they match together. This class specifically is the main class of the program. The problem comes when i enter a string like "Abey," and I'll get the error.

ERROR: Please give the student name: Abey java.io.FileNotFoundException: Abey (The system cannot find the file specified)

MY CODE

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);
  }

 }

}
Arpan Sarkar
  • 2,301
  • 2
  • 13
  • 24
  • 3
    Too many blank lines and no indentation. Please format your code properly. From [`FileReader`'s constructor documentation](https://docs.oracle.com/javase/9/docs/api/java/io/FileReader.html#FileReader-java.lang.String-): "*[throws] `FileNotFoundException` - if the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.*" – Turing85 Apr 22 '18 at 17:33
  • try to give full path of file, or keep the file and program in same dir – Roushan Apr 22 '18 at 17:35
  • And you definitely have a file that matches in the correct location? That's what the `FileNotFoundException` is telling you – MrB Apr 22 '18 at 17:35
  • Update: So basically my problem was that i was entering "Abey" which is hte name of one of the students in my file, when i wasn't entering the actual file name which was "Students.txt" This guys in this link was having the same problem if i caused any confusion. https://stackoverflow.com/questions/16921784/running-a-java-program-in-eclipse-as-if-from-command-line?lq=1 I did what the guys in his post said to do which was change the files from the source to the main program file itself and that took away the "FileNotFoundException" but instead gave me the "NoSuchElementException" – Jerimiah Smith Apr 25 '18 at 21:46

2 Answers2

0

The process cannot find the Abey file relative to the working directory. Try to specify the full path:

File root = new File("/path/to/data/files");
...
String filename = ....;
File datafile = new File(root, filename);
try (FileReader reader = new FileReader(datafile)) {
  ....
}
M. le Rutte
  • 3,525
  • 3
  • 18
  • 31
-1

The main Problem is, Program is searching as relative path. You need to provide the complete path of the file.

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));

This will be the modified code for you.

Here completePath variable contains path of the folder on which you have stored files by student name.

Naresh Bharadwaj
  • 192
  • 1
  • 12