0

I am writing a program for an assignment that uses a jFrame UI to read the contents of a notepad file full of "grades" (A, B, C, C+ etc) and then calculate the total number of grades, and the highest and lowest grades. In the jFrame, the user inputs a file name (I do not want to use JFileChooser because that is not part of the assignment) and clicks "load" to load the file. I am struggling with the Load() method. So far, I have:

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

    ArrayList<LetterGrade> grades = new ArrayList();

    try{
        Scanner input = new Scanner(file);
        String grade = input.nextLine();
        grades.add(LetterGrade.grade);
    } catch (FileNotFoundException ex) {
        System.out.printf("ERROR: %s", ex);
    }

    return grades;

I know that there is a problem in the line that says grades.add(LetterGrade.grade) because I can only add a LetterGrade enum (A, AMinus, BPlus, B etc) to the ArrayList "grades" whereas grade is a String. How can I make it so that when Scanner reads the file, it takes each grade and adds it to the ArrayList called "grades"? Also, I only want to take grades that are part of my LetterGrade enum, so would I use an if/else to filter that? Thanks for the help, and I will respond as quickly as possible to questions.

James Curtis
  • 31
  • 1
  • 1
  • 4
  • You don't show the type of variable `LetterGrade.grade`, and you never told us exactly what you're struggling with – Vince Dec 01 '14 at 01:16
  • possible duplicate of [Java - Convert String to enum](http://stackoverflow.com/questions/604424/java-convert-string-to-enum) – APerson Dec 01 '14 at 01:21
  • @VinceEmigh `LetterGrade.grade` is probably OP's attempt at indexing into `LetterGrade`, and there's a question at the bottom. – APerson Dec 01 '14 at 01:22
  • @APerson Nevermind, I just saw where he mentioned it was a String. If his question were better formatted, and didn't include irrelevant info, I would have seen it more clearly – Vince Dec 01 '14 at 02:13

2 Answers2

1

Java isn't able to automatically convert a String with the value of a LetterGrade directly into a LetterGrade. You can write that function yourself. Try doing something like this:

LetterGrade parseGrade(String grade) {
    if(grade.equals("A") return LetterGrade.A;
    // and so on
}

And then, instead of

grades.add(LetterGrade.grade);

in Load(), you would have this:

grades.add(parseGrade(grade));

If you want to check that what you're adding is a valid grade first, have parseGrade() return null if it's an invalid grade, then check for null while adding the grade to grades.

APerson
  • 8,140
  • 8
  • 35
  • 49
  • Actually, there is a method to convert a string to an `Enum` already. It's `LetterGrade.valueOf(String)`. – khelwood Dec 01 '14 at 01:17
  • Yes, but what if the letter grades from the file aren't exactly equal to the enumeration values? `valueOf()` is very particular about whitespace and other issues. – APerson Dec 01 '14 at 01:20
  • if it's whitespace that's the issue, you can `trim` the string before using `valueOf`. If the string is significantly different, I agree you might have to parse it manually. – khelwood Dec 01 '14 at 07:50
1

Change LetterGrade.grade to

LetterGrade.valueOf(grade);

LetterGrade.grade would be used to access a variable named grade in the scope of LetterGrade.

You need to convert the String to Enum.

See Lookup enum by string value for more detials

Community
  • 1
  • 1
Chris Manning
  • 499
  • 5
  • 14