'JAVA: Using String in Do While for decision

import java.util.Scanner;
import java.util.*;

public class MultipicationTable{

    public static void main(String[] args)
    {
        // Initialising selection variable to 0 
        int selection = 0;
        int MultiValue = 0;
        int UserValue = 0;

        // Initializing the count for loop
        int i;


        // Initializing Random1 and Random2 to get random values
        int Random1;
        int Random2;


        // Creating new Scanner
        Scanner input = new Scanner(System.in);

        do{

        // Menu to select type of multipication
        System.out.println("Please select your option");
        System.out.println("1: Random Multipication table");
        System.out.println("2: Give your own numbers");

        // Getting the input from the above menu
        selection = input.nextInt();

        switch (selection)
        {
            case 1:


                Random1 = (int)(Math.random() * 1000);
                Random2 = (int)(Math.random() * 1000);

                Random1 = Random1/10;

                System.out.println("You Random Value to pultiply is " + Random1);


                System.out.println("How long do you want? 2 - 100 ");


                MultiValue = input.nextInt();


                for (i = 1; i <= MultiValue; i++ )
                {
                    System.out.println("Multipication of " + Random1 + " * " + i + " is: " + Random1 * i);
                }



            case 2:

                System.out.println("What is your Number? ");
                UserValue = input.nextInt();

                System.out.println("How long do you want to multiply? ");
                MultiValue = input.nextInt();

                for (i = 1; i <= MultiValue; i++ )
                {
                    System.out.println("Multipication of " + UserValue + " * " + i + " is: " + UserValue * i);
                }



        }

        System.out.println("Would you like to exit? ");
        String Exit = input.nextLine();

        }while(Exit != 'y');    

    }



}

I think my error is on this part of the code.

    System.out.println("Would you like to exit? ");
    String Exit = input.nextLine();

}while(Exit != 'y');    

I get an error like "Exit cannot be resolved". My aim is to loop until the user enters y in that question.



Solution 1:[1]

First of all, the condition in a while or do...while loop is in the outer scope. This means that any variables declared within the block of the do...while are no longer in scope when the condition is checked. To solve this problem, you need to declare your exit variable outside of the do...while loop:

String Exit = null;

do {
    // do something
    Exit = input.nextLine();
} while (Exit != 'y');

However, this code will still fail to compile because Exit is declared as a String but you are comparing it with 'y' which is a char. String literals are always surrounded by double-quotes, so "y" is a String.

Now changing the condition to Exit != "y" will compile, but it will not run as expected. You need to compare Strings using the equals() method. This means that the condition should be !Exit.equals("y"). Placing this inside the while condition should fix the problems with your loop.

Alternatively, if you want to check for the word "yes" or variants, you can use while(Exit.charAt(0) != 'y');. This checks to see if the first character in Exit is a 'y' character.

Solution 2:[2]

String Exit has to be declared outside the loop

String Exit =null;
do {
//body
Exit= input.next(); // or input.nextLine();
} while(!Exit.equals("y"));

Edited as mentioned by Kartik : Should use equals to compare strings. and == checks if both variables refer to same object.

Solution 3:[3]

Your first problem is the scope of Exit as @Subin has covered.You might want to use }while(!Exit.equals("y"); as well.

You can look at How do I compare strings in Java? for the reason. Basically your != will likely always be true since it compares objects and thus will decide that they are two different objects.

Solution 4:[4]

/This is easy code using do-while loop/

    String  option;
    do {
        System.out.println("Enter your Choice : ");
        option=predator.next();
        switch (option) {
            case "add":
                System.out.println("Addition : " + (a + b));
                break;
            case "sub":
                System.out.println("Subtraction : " + (a - b));
                break;
            case "mult":
                System.out.println("Multiplication : " + (a * b));
                break;
            case "div":
                System.out.println("Division : " + (a / b));
                break;
            default:
                System.out.println("You have entered wrong keyword : "+option);
        }
    }
    while (!option.equals("Quit"));

Solution 5:[5]

    System.out.println("Would you like to exit? ");
    String Exit = input.nextLine();

}while(!Exit.equals('y');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Community
Solution 4 prosenjit paul
Solution 5 Michael 'Maik' Ardan