-1

I am trying to write code in Java to read a file that a user has input the name of. I have two classes, P5.java and GradeFileLoader.java, and I am trying to implement the Load() method from GradeFileLoader in the P5.java class to load a file with a name that was input by the user. Here is the code that I have:

private void jButtonProcessActionPerformed(java.awt.event.ActionEvent evt) {                                               
    String studentFileInput = this.jTextFieldGradeFileInput.getText();
    GradeFileLoader.Load(studentFileInput);

The Load() method has this code inside of it:

public ArrayList<LetterGrade> Load(File file) throws FileNotFoundException {

    ArrayList<LetterGrade> grades = new ArrayList();
    for(int i=0; i<grades.size(); i++){
        try{
            Scanner input = new Scanner(file);
            String grade = input.nextLine();
            grades.add(LetterGrade.valueOf(grade));
        } catch (FileNotFoundException ex) {
        System.out.printf("ERROR: %s", ex);
        }   
    }

    return grades;
}

}

My question is this: How can I make Java recognize that the String studentFileInput is the file that I want to be loaded? Thanks for the help.

PS: I do not want to use JFileChooser

James Curtis
  • 29
  • 1
  • 1
  • 2

1 Answers1

-1

Instead of Scanner use File Reader

FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
String grade = br.readLine();

You have to give the file with extentsion (eg:- "test.txt")

janitha000
  • 602
  • 8
  • 29