-1

I just started to learn Java, and I'm a bit lost as to why my "break" command at the bottom of the code is being executed.

import java.util.Scanner;

public class GradeValues {

    private static Scanner scanner;

    public static void main(String[] args) {
        boolean keepGoing = true;
        String grade; // initializing variable for grade

        while(keepGoing = true){
            System.out.println("What percentage did you get? ");  //Ask User the question
            scanner = new Scanner(System.in); //starting the scanner 
            grade = scanner.nextLine();  //storing user input of percentage

            int percentage = Integer.parseInt(grade);  //converting string to a int

            if(percentage >= 80 && percentage <= 100){
                System.out.println("Your grade is an A!  Awesome job!");
            }else if(percentage >= 70 && percentage <= 79){
                System.out.println("Your grade is an B!  Nice work!");
            }else if(percentage >= 60 && percentage <= 69){
                System.out.println("Your grade is an C!  That's average. =( ");
            }else if(percentage >= 50 && percentage <= 59){
                System.out.println("Your grade is an D!  Come on man, you can do better.");
            }else if(percentage < 50){
                System.out.println("Your grade is an F!  You need to hit the books again and try again.");
            }else{
                System.out.println("I think you type the wrong value.");
            }

            System.out.println("Would you like to check another grade?");  //Asking user if they want to do it again
            Scanner choice = new Scanner(System.in);  //Gets user input
            String answer = choice.nextLine();  // Stores input in variable "answer"
            if(answer == "yes"){
                keepGoing = true;  //While loop should keep going

            }else{
                keepGoing = false;  //While loop should stop
                break;  //this should stop program
            }
        }
    }
}

Considering that the keepGoing boolean variable is still true (if the user types 'yes'), the application will still stop because of the break in the else statement. Can someone let me know why it's doing that and how to fix that?

resueman
  • 10,572
  • 10
  • 31
  • 45
Tscott
  • 465
  • 6
  • 21

3 Answers3

2

You cannot compare string with == operator.

In order to compare the strings correctly you have to use equals method.

For example replacing in your code the if/else statement with:

if("yes".equals(answer)){
    keepGoing = true;  //While loop should keep going

}else{
    keepGoing = false;  //While loop should stop
    break;  //this should stop program 
}
antoniodvr
  • 1,259
  • 1
  • 14
  • 15
1

I guess if its not typo then your while condition is not correct while(keepGoing = true) ... should be while(keepGoing == true) or while(keepGoing). And with that you don't need to break at end. And like suggested in other answers please use equals to compare strings.

ravinikam
  • 3,666
  • 6
  • 28
  • 25
0

answer is a String, which is a type of object. You should compare it with the equals method, not the == operator:

if (answer.equals("yes")) {
    // etc.
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 2
    this is not correct at all. When you have a constant value the best thing is put this one as this parameter: `"yes".equals(answer)` in order to not generate random nullpointer in the code. – antoniodvr Sep 19 '15 at 18:40
  • `answer` could be null, and you'l get NPE – msangel Sep 19 '15 at 18:42
  • @VoodooCoder `answer` is never going to be null. `Scanner.nextLine` throws an exception if there's no next line. – resueman Sep 19 '15 at 18:47
  • It's a good java programming rule. If in the future the answer will be retrieved in any other way you will not have to do any refactor to your code! – antoniodvr Sep 19 '15 at 18:54
  • @resueman, its good that you remember this all the time. but I'm not, and making constatnt first will always work for me. – msangel Sep 19 '15 at 18:55
  • @VoodooCoder but calling the answer "not correct at all" when it works correctly is a massive exaggeration. – resueman Sep 19 '15 at 18:56