Brandon Haley
Brandon Haley

Reputation: 11

Letter grade to number grade using switch/if statements?

I am tasked with creating a java program where the user inputs a grade (D- through A+) and gets a score (0.7 through 4.3)

So far it works ONLY if the user doesn't add a "+" or "-".

So if the user inputs ONLY a letter, it works.

For some odd reason, if the user inputs a "+" or a "-" after a letter, the output would be "0.3" or "-0.3" respectfully.

I'm not sure why. My AP Comp Sci teacher and I are stumped.

Here is a relevant snip of my code

String letterGrade;
double numericGrade;
Scanner reader = new Scanner(System.in);
public boolean getLetterGrade()
{
    System.out.print("Enter a letter grade: ");
    letterGrade = reader.nextLine();
    return (letterGrade.length() < 3) && (("+".equals(letterGrade.substring(1))) || ("-".equals(letterGrade.substring(1))) || (letterGrade.length() == 1));
}
public double getNumericGrade()
{
    numericGrade = 0.0;
    switch(letterGrade.substring(0).toUpperCase())
    {
        case "A":
            numericGrade += 4.0;
            break;
        case "B":
            numericGrade += 3.0;
            break;
        case "C":
            numericGrade += 2.0;
            break;
        case "D":
            numericGrade += 1.0;
            break;
        case "F":
            numericGrade += 0.0;
            break;
    }
    if ("-".equals(letterGrade.substring(1)))
        numericGrade -= 0.3;
    if ("+".equals(letterGrade.substring(1)))
        numericGrade += 0.3;
    return numericGrade;
}

My guess is that numericGrade is getting reset to 0 after the switch statement, for no reason apparent to me.

Upvotes: 1

Views: 2360

Answers (2)

choppe
choppe

Reputation: 506

Try this, A simple grade interpreter. If you want to understand more in to details of such problems , you could check with Interpreter design pattern

public class Grades {
private double score = 0;
public double calculate(char c) throws IllegalArgumentException{
    switch(c){
        case 'A':
            score += 4; 
            break;
        case 'B':
            score += 3;
            break;
        case 'C':
            score += 2;
            break;
        case 'D':
            score += 1;
            break;
        case '+':
            score += 0.3;
            break;
        case '-':
            score -= 0.3;
            break;
        default:
            throw new IllegalArgumentException("Invalid Argument");
    }

    return score;
}

public static void main(String args[]){
    Scanner reader = new Scanner(System.in);
    Grades grade = new Grades();
    String line = reader.nextLine();
    while(!line.equals("exit")){

        //validate argument here..
        double value;
        for ( int i = 0; i < line.length(); i++){
            value = grade.calculate(line.charAt(i));

        }
        System.out.println("Grade = " + value);
        line = reader.nextLine();
    }
    reader.close();
}

Upvotes: 0

phflack
phflack

Reputation: 2727

String.substring(int position) returns the sub-string from position onwards, if you use "1234".substring(1), it will return "234"

In this case you're using "C+".substring(0), which returns C+ and not C, you need either String.charAt(0) or String.substring(0, 1)

That's all that is needed to fix it for +/-, but do be aware that if the string entered is of length one (just a letter grade, A/B/C/D/F), then your program will crash because your if statements try to access the second character. You should check the length and return early if the string entered is too short

Upvotes: 1

Related Questions