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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user1947402
  • 31
  • 1
  • 1
  • 4
  • 8
    By convention, variables should be lowercase and you would need to invoke the .equals function to properly perform String comparisons. – Anthony Forloney Jan 04 '13 at 03:33

5 Answers5

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 2
    Comment on last paragraph: what if the input is "Yikes! No way!"? :P – tckmn Jan 04 '13 at 03:58
  • 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. – user1947402 Jan 04 '13 at 06:30
  • 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. – wullxz Jan 04 '13 at 08:22
  • @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. – Code-Apprentice Jan 04 '13 at 23:30
  • @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. – Code-Apprentice Jan 04 '13 at 23:32
  • @user1947402 *"It does display if I want to exit but then it displays menu directly."* This sounds like your calls to nextInt are orphaning a new line character. Basically you need to make an empty call to nextLine after calling nextInt and it should work then. Question that's similar to that issue: http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – Radiodef Jan 13 '14 at 18:00
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.

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
  • 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. – user1947402 Jan 04 '13 at 06:29
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.

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
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"));
-1
    System.out.println("Would you like to exit? ");
    String Exit = input.nextLine();

}while(!Exit.equals('y');
Michael 'Maik' Ardan
  • 4,213
  • 9
  • 37
  • 60
  • 1. Syntax error 2. Scope is local 3. Probably copied from Karthik's answer 4. downvoted – tckmn Jan 04 '13 at 03:59