0

I've been messing around way too long on this one and would like to get more out of my if else statements. All of the code works up until a student has a low GPA and or a low SAT score but happens to be the valedictorian of a huge class. I know its unheard of but my code isn't working correct to me if it doesn't qualify that student.

My first post. Thanks for any suggestions Bryan

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    System.out.println("\n\tStudent Qualifier");

    System.out.println("Enter students GPA: ");
    double gpa = in.nextDouble();

    System.out.println("Enter students SAT score: ");
    double sat = in.nextDouble();

    System.out.println("\nIf student was valedictorin or salutatorian of a school of 1400 or more, Enter y or n");
    String highestHonors = in.next();
    in.close();     

    if (gpa >= 4.0 && sat >= 1100) 
        studentQualified();
    if (gpa >= 3.5 && sat >= 1300) 
        studentQualified(); 
    if (gpa >= 3.0 && sat >= 1500) 
        studentQualified();
    if (highestHonors == "y")
        studentQualified();
    else
        unQualified();
    }

public static void studentQualified() {
    System.out.println("Student is qualified");
}
public static void unQualified() {
    System.out.println("Student is not qualified");
}

}

Bryan
  • 11
  • 3
  • 3
    Dry run your code. TIp: always use curly braces, even if it is a single line after if condition – Jude Niroshan Jul 20 '17 at 04:29
  • 1
    Welcome to Stack Overflow! [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Jul 20 '17 at 04:35

2 Answers2

2

You need to use equals() to compare strings, not ==. == will check if the strings are literally the same (i.e. occupy the same space in memory), whereas equals() merely checks if the characters are the same.

Something like this should work:

if ("y".equals(highestHonors))

EDIT: Also, please use curly braces even if you don't think you need them. It makes the code look better and prevents errors when someone wants to add another branch.

itsmichaelwang
  • 2,282
  • 4
  • 16
  • 25
  • Ok cool. I've read that and still have to learn thru running into it head first I guess. Thank You – Bryan Jul 20 '17 at 04:49
2

You can use || to combine your tests. Don't use == for comparing String(s) - you can use String#equalsIgnoreCase(String). Also, I would highly recommend you always use {} with if-else(s). Like,

if ((gpa >= 4.0 && sat >= 1100) || (gpa >= 3.5 && sat >= 1300) ||
        (gpa >= 3.0 && sat >= 1500) || highestHonors.equalsIgnoreCase("y")) {
    studentQualified();
} else {
    unQualified();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Great! I thought about using the || like that but didn't try it. I did it the first time with just two of the statements within one set of parenthesis and that didn't cut it. :-) Thank you so much. I love this stuff and have been wearing myself out on the little issues that end up being simple. – Bryan Jul 20 '17 at 04:47