0

I just have to make a method that checks if three integers are the same, have the user tell an object to execute this method, and then ask the user for another command. When I put 'c' (it's ONLY this case that does this, I have another case that is logically identical), it will do what it's supposed to but then it will try to take the next input and run it through as a parameter to the method that has already been executed, from what I understand.

    something=in.nextLine();
    commands=something.charAt(0);
    do{
        switch(commands){
            //Blah blah blah other commands
            case 'c':
                boolean yes=object.allTheSame(in.nextInt(),in.nextInt(),in.nextInt());
                System.out.println(yes);
                System.out.println("Please enter a command: ");
                something=in.nextLine();
            break;
        }
        commands=something.charAt(0);
    }while(commands!='q'); 
Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
Taylor
  • 23
  • 1
  • 3
  • You might be running into [this problem](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx). If that's the case, just put another `in.nextLine();` on its own line before `something=in.nextLine();`. – sgbj Nov 01 '13 at 18:35

2 Answers2

1

The break you have in there breaks the switch statement and not the do-while loop.

(As your comparisons seem trivial, I think it's better form to use the tradition if-else.)

skytreader
  • 11,467
  • 7
  • 43
  • 61
0

I don't know what's in the "Blah blah blah other commands" part, but if the first character of something is not c, then commands will apparently never change. You might want to do this:

something=in.nextLine();
int pos = 0;
commands=something.charAt(pos++);
do{
    switch(commands){
        //Blah blah blah other commands
        case 'c':
            boolean yes=object.allTheSame(in.nextInt(),in.nextInt(),in.nextInt());
            System.out.println(yes);
            System.out.println("Please enter a command: ");
            something=in.nextLine();
        break;
    }
    commands=something.charAt(pos++);
}while(commands!='q'); 
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521