3
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.

1
  • 8
    By convention, variables should be lowercase and you would need to invoke the .equals function to properly perform String comparisons. Commented Jan 4, 2013 at 3:33

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Comment on last paragraph: what if the input is "Yikes! No way!"? :P
The code by @Code-Guru worked!... But there is no loop...It goes directly to Menu again without asking to me to quit. It does display if I want to exit but then it displays menu directly. It looks like Exit = input.nextLine(); was not executed properly.
add System.out.println("Exit value: " + Exit); after Exit = input.nextLine(); to see what's the value of Exit. Maybe that will make it clear. By the way: It is no Syntaxrule but it is convention to start variables with a lower case letter. Only classes and constants (final) should start with an upper case letter. And tell us, what you've changed in your while condition.
@Doorknob That was someone else's addition. For a school assignment like this seems to be, it seems like a reasonable way to go. I probably wouldn't do it myself, though.
@user1947402 Please edit your question or start a new one with the changes you have made and explain what the current problem is. We will be glad to help.
|
1

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.

1 Comment

The code by @Code-Guru worked!... But there is no loop...It goes directly to Menu again without asking to me to quit. It does display if I want to exit but then it displays menu directly. It looks like Exit = input.nextLine(); was not executed properly.
0

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.

Comments

0

/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"));

Comments

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

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

1 Comment

1. Syntax error 2. Scope is local 3. Probably copied from Karthik's answer 4. downvoted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.