0

So I just have a quick little issue

 int pickmeup = 0;
        while (true)
        {
        pickmeup = scanner.nextInt();
        if (pickmeup == 1)
         {System.out.println ("you entered 1");}
        if(pickmeup == 2)
         {System.out.println ("you entered 2");}
        {
            break;
        }
    System.out.println ("Invalid code");

Now when I run this code it all works fine however in regards to the strings but it seems as though the loop doesn't work all that well when I enter '3', as it doesn't return the string 'Invalid code'.

If I were to get rid of the strings after both if statements, then it works perfectly fine. What exactly am I doing wrong? Are there other ways to automatically have strings output?

7
  • This code doesn't compile because of mismatched {. Commented Oct 18, 2014 at 22:36
  • ((pickmeup == 1) is this ur real code Commented Oct 18, 2014 at 22:36
  • Sorry, I closed the brackets on the strings, if that's what you meant. Commented Oct 18, 2014 at 22:37
  • @getlost I've edited it to reflect my actual code. I made last minute changes and changed it back and forgot to edit it correctly. Commented Oct 18, 2014 at 22:38
  • Then fix your code first. Second if if(pickmeup == 2) is currently inside of the first one, just like the line System.out.println ("Invalid code");. Commented Oct 18, 2014 at 22:40

2 Answers 2

1

I believe you want to use a logical or || and an else like,

int pickmeup;
while (true) {
    pickmeup = scanner.nextInt();
    if (pickmeup == 1 || pickmeup == 2) {
        System.out.printf("you entered %d%n", pickmeup);
    } else {
        System.out.println("Invalid code");
    }
}

Alternatively, you could use an else if chain like,

int pickmeup;
while (true) {
    pickmeup = scanner.nextInt();
    if (pickmeup == 1) {
        System.out.println("you entered 1");
    } else if (pickmeup == 2) {
        System.out.println("you entered 2");
    } else {
        System.out.println("Invalid code");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Its funny, I did try an or statement, but I had issues figuring out how to make the strings work for it. This looks promising. Thanks!
0

You could start with firstly correcting your code, you can do that in eclipse Source-Format or by pressing CTRL+SHIFT+F

For your example, I corrected as much as I understood, currently it breaks only if else is reached. Break can be modified.

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int pickmeup = 0;
    while (true){
        pickmeup = scanner.nextInt();
        if (pickmeup == 1){
            System.out.println("one");
        }
        else if (pickmeup == 2){
            System.out.println("two");
        }
        else{
            System.out.println("Invalid code");
            break;
        }
    }

Comments

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.