1

GPA Calculator

I know there are similar threads out there but none that i have seen have come across the problem I am experiencing. I am receiving no errors yet my final answer is always 0 instead of an actual GPA value. Any help would be greatly appreciated! Note: I am new to Java this is my first official project after hours of reading about Java.

public static void main (String[]args){


    double total = 0,total2=0, total3=0, total4=0, total5=0, total6=0;
    double classes=0;
    double answer=0;
    String grade1,grade2,grade3,grade4,grade5,grade6;
    int p1, p2, p3, p4, p5, p6;



System.out.println("Welcome to the Gpa Calc");
System.out.println("Please enter a grade for every class you have.");
System.out.println("Shall we begin?");
System.out.println("How many classes do you have?");
    Scanner c = new Scanner(System.in);
    classes= c.nextInt();

    Scanner grade = new Scanner(System.in);
    System.out.println("What is your Grade in your first period class?");
    grade1 = grade.nextLine();
    if(grade1 == "A" || grade1 =="a"){
         p1 = 4;
         total+=p1;
    }
    else if(grade1 == "B" || grade1 =="b"){
        p1= 3;
        total+= p1;
    }
    else if(grade1 == "C" || grade1 =="c"){
        p1= 2;
        total+= p1;
    }
    else if(grade1 == "D" || grade1 =="d"){
        p1= 1;
        total += p1;
    }
    else if(grade1 == "F" || grade1 =="f"){
        p1= 0;
        total += p1;
    }   

    System.out.println("What is your Grade in your second period class?");
    grade2 = grade.nextLine();

    if(grade2 == "A" || grade2 =="a"){
         p2 = 4;
         total2 += p2;
    }
    else if(grade2 == "B" || grade2 =="b"){
         p2 = 3;
         total2 += p2;
    }
    else if(grade2 == "C" || grade2 =="c"){
         p2 = 2;
         total2 += p2;
    }
    else if(grade2 == "D" || grade2 =="d"){
         p2 = 1;
         total2 += p2;
    }
    else if(grade2 == "F" || grade2 =="f"){
         p2 = 0;
         total2 += p2;
    }   

    System.out.println("What is your Grade in your third period class?");
    grade3 = grade.nextLine();

    if(grade3 == "A" || grade3 =="a"){
         p3 = 4;
         total3 += p3;
    }
    else if(grade3 == "B" || grade3 =="b"){
         p3 = 3;
         total3 += p3;
    }
    else if(grade3 == "C" || grade3 =="c"){
         p3 = 2;
         total3 += p3;
    }
    else if(grade3 == "D" || grade3 =="d"){
         p3 = 1;
         total3 += p3;
    }
    else if(grade3 == "F" || grade3 =="f"){
         p3 = 0;
         total3 += p3;
    }   

    System.out.println("What is your Grade in your fourth period class?");
    grade4 = grade.nextLine();

    if(grade4 == "A" || grade4 =="a"){
         p4 = 4;
         total4 += p4;
    }
    else if(grade4 == "B" || grade4 =="b"){
         p4 = 3;
         total4 += p4;
    }
    else if(grade4 == "C" || grade4 =="c"){
         p4 = 2;
         total4 += p4;
    }
    else if(grade4 == "D" || grade4 =="d"){
         p4 = 1;
         total4 += p4;
    }
    else if(grade4 == "F" || grade4 =="f"){
         p4 = 0;
         total4 += p4;
    }   

    System.out.println("What is your Grade in your fifth period class?");
    grade5 = grade.nextLine();

    if(grade5 == "A" || grade5 =="a"){
         p5 = 4;
         total5 += p5;
    }
    else if(grade5 == "B" || grade5 =="b"){
         p5 = 3;
         total5 += p5;
    }
    else if(grade5 == "C" || grade5 =="c"){
         p5 = 2;
         total5 += p5;
    }
    else if(grade5 == "D" || grade5 =="d"){
         p5 = 1;
         total5 += p5;
    }
    else if(grade5 == "F" || grade5 =="f"){
         p5 = 0;
         total5 += p5;
    }   
    System.out.println("What is your Grade in your sixth period class?");
    grade6 = grade.nextLine();

    if(grade6 == "A" || grade6 =="a"){
         p6 = 4;
         total6 += p6;
    }
    else if(grade6 == "B" || grade6 =="b"){
         p6 = 3;
         total6 += p6;
    }
    else if(grade6 == "C" || grade6 =="c"){
         p6 = 2;
         total6 += p6;
    }
    else if(grade6 == "D" || grade6 =="d"){
         p6 = 1;
         total6 += p6;
    }
    else if(grade6 == "F" || grade6 =="f"){
         p6 = 0;
         total6 += p6;
    }   


    answer = total + total2 + total3 + total4 + total5 + total6 ;
    double GPA = answer/classes;
    System.out.println("Your current unweighed GPA is " +GPA);
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123

3 Answers3

1

You need to use grade1.isEquals("A") to compare the strings. grade1 == "A" compares the object identity, which will be false since they are different objects. You might also consider doing away with the long 'if' chains and instead go with:

answer += grade1.toLowerCase().indexOf("fdcba"); // returns the zero based index 0-4 for f-a

it's a little more succinct.

1

Because you are using a single character to comparison you can replace your comparison code with this:

  char grade1 = grade.nextLine().charAt(0);
       if(grade11 == 'A' || grade11 =='a'){
            // 
        }

NOTE:And also try to make your code modular. Create a method that accepts a grade param and return the gpa and get ride all the repeating codes

ambes
  • 5,272
  • 4
  • 26
  • 36
0

So what is happening is that none of your conditionals are ever being met because you are using ==. That would work fine when comparing ints but Strings are a little bit different. In basic terms == checks if the location of the data is the same. You need to use equals() instead because it checks if the value of the data is the same. For example

if(grade1.equals("A") || grade1.equals("a")){
     p1 = 4;
     total+=p1;
}

This post has some excellent answers describing the difference between == and equals.

Community
  • 1
  • 1
Lubed Up Slug
  • 168
  • 1
  • 11
  • Thanks to everyone who has commented so far! You have no idea how frustrating this was for me as i am new to Java! Its evident my issue was related to the usage of `==` instead of `equals` . I've furthered my knowledge on Java because of you all. Thanks! @LubedUpSlug @Randy Kamradt Sr. @ambes – Trashpicker Feb 22 '15 at 09:39