0

Figured it out. Thanks guys. I know its not a perfect answer but, I'm new to java.

package Dr._Coles_Practice_Problems;

import java.util.Scanner;

public class GpaCalc {

public static void main(String[] args) {

   // Needed variables 

    double credits = 0.0;

    double totalcredits = 0.0;

    double grade = 0.0;

    double qualitypoints = 0;

    int classes;

    String lettergrade;

    Scanner input = new Scanner(System.in);

    System.out.println("Enter the amount of classes for this semester");

    classes = input.nextInt();

    while (classes > 0) {

    System.out.println("Enter the amount of credits for this class");

    credits = input.nextDouble();

    System.out.println("Enter the grade achieved for this class");

    lettergrade = input.next();

        if (lettergrade.equalsIgnoreCase("a")) {

            grade += 4.00;

        }

        if (lettergrade.equalsIgnoreCase("a-")) {

            grade += 3.67;

        }

        if (lettergrade.equalsIgnoreCase("b+")) {

            grade += 3.33;

        }

        if (lettergrade.equalsIgnoreCase("b")) {

            grade += 3.00;

        }

        if (lettergrade.equalsIgnoreCase("b-")) {

            grade += 2.67;

        }

        if (lettergrade.equalsIgnoreCase("c+")) {

            grade += 2.33;

        }

        if (lettergrade.equalsIgnoreCase("c")) {

            grade += 2.00;

        }

        if (lettergrade.equalsIgnoreCase("c-")) {

            grade += 1.67;

        }

        if (lettergrade.equalsIgnoreCase("d+")) {

            grade += 1.33;

        }

        if (lettergrade.equalsIgnoreCase("d")) {

            grade += 1.00;

        }

        if (lettergrade.equalsIgnoreCase("f")) {

            grade += 0.00;

        }

        classes--;

        totalcredits += credits;  

    }      

     qualitypoints = grade * credits;   

    // GPA is calculated via total credits / total quality points 
   //of which are calculated by multiplying the credit(s) * the quality point value


    System.out.println("Your total credit(s)are: " + totalcredits);

    System.out.println("Your total quality points are: " + qualitypoints);

    System.out.println("Your GPA is: " + (qualitypoints / totalcredits));
}

}

/*

Bloomsburg University's grading scale

A = 4.00 Superior Attainment

A- = 3.67

B+ = 3.33

B = 3.00 Above Average Attainment

B- = 2.67

C+ = 2.33

C = 2.00 Average Attainment

C- = 1.67

D+=1.33

D = 1.00 Minimum Attainment

F = 0.00 Failure

*/

Espryon
  • 19
  • 2
  • 2
    FYI, in the future it would help if you said what wasn't right (compile error, runtime error, incorrect output, etc.) – Paul Draper Nov 23 '13 at 09:32
  • I was just doing what the autocorrect said to me. Below 15 badges... edit your post otherwise wait 6 hours to post the final version of it . – Espryon Nov 24 '13 at 04:58

5 Answers5

4
 if (grade.toLowerCase() == "a") {

Since "a" and grade.toLowerCase() are objects (of type String), comparing them wih == compares the objects' references, not the content of the String.

Use equals() method.

if (grade.toLowerCase().equals("a")) {

More over use equalsIgnoreCase method to ignore the case.

if (grade.equalsIgnoreCase("a")) {
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

In addition to Suresh Atta's answer, I would suggest that you use the method shown below to compare String :

"a".equals(grade){
  // something
}  

The reason being, it protects you against NullPointerExceptions.

An SO User
  • 24,612
  • 35
  • 133
  • 221
0

Object comparison in Java using == goes by object reference, not by content. Use equals() or equalsIgnoreCase() to compare strings, depending on your needs.

Krease
  • 15,805
  • 8
  • 54
  • 86
0

You're dividing by zero.

Ignoring a portion of your code, you're basically doing this:

while (credits / 3 > 0) {
  credits = credits - 3;
}
grades / credits;

When your while loop finishes, credits will be <=0.

zifnab06
  • 76
  • 3
0

As several others have pointed out already, you must use equals() instead of == to compare Strings.

To make your code more compact and readable, you could also use a switch statement:

switch (grade.toLowerCase()) {

    case "a":
        grades += 4.00;
        break;

    case "a-":
        grades += 3.67;
        break;

    /* etc. */

}
n.st
  • 946
  • 12
  • 27