Home > Net >  Scanner with While loop and If-Else statement (keep getting thrown out the loop)
Scanner with While loop and If-Else statement (keep getting thrown out the loop)

Time:11-25

I just wanted to make a simple something, the functionality is as follow:

Scenario 1

  • Ask user for 10 numbers to add with a "#" to signify the order of number the user is inputting.
  • If the user did not put a number, it will say it's invalid and ask for another number (with the same order #}.
  • Sums up all number.

I was able to make scenario 1 happen with this:

public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int counter = 1;
        int sum = 0;

        while (true){
            System.out.println("Enter a number #"   counter   ":");
            boolean num = scanner.hasNextInt();
            if (num){
                counter  ;
                int enter = scanner.nextInt();
                sum  = enter;
                if (counter > 10){
                    System.out.println("Your total is: "   sum);
                    break;
                }
            }else {
                System.out.println("Invalid Number");

            }

            scanner.nextLine();
        }
        scanner.close();

    }

Scenario 2 For this one, instead of the code just asking the user to try again when they input an invalid number, I want the user to have a choice to either continue or exit the program. I made some changes to the code which I thought would work but it just keeps on kicking me out the program all together.

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    int counter = 1;
    int sum = 0;

    while (true){
        System.out.println("Enter a number #"   counter   ":");
        boolean num = scanner.hasNextInt();
        if (num){
            counter  ;
            int enter = scanner.nextInt();
            sum  = enter;
            if (counter > 10){
                System.out.println("Your total is: "   sum);
                break;
            }
        }else {
            System.out.println("Invalid Number");
            System.out.println("Do you want to continue? Y/N: ");
            String answer = scanner.nextLine();
            scanner.nextLine();
            if (Objects.equals(answer, "Y")){
                continue;
            }else {
                break;
            }
        }

        scanner.nextLine();
    }
    scanner.close();

}

I expect the program to take Y for continue and N to exit the program. Tried to add another if-else statement inside the else statement.

This is what I get, it just takes why and exits.

enter image description here

CodePudding user response:

you are using equals method which will return false for 'Y' equals to 'y' as it is case sensitive. Instead you can use equalsIgnoreCase(), this method is case insensitive.

CodePudding user response:

I'm not too sure but doesn't this:

String answer = scanner.nextLine();
scanner.nextLine();

scan nextLine in console twice?

Also, personally I would do:

String a = scanner.nextline();
if(a.toLowerCase()=="y"){
yes}else{
no}
  • Related