0

I am creating a program written in Java using Eclipse, and JDK 8. I have created a Scanner using this code: Scanner input = new Scanner(System.in);

I then created a String using the code String command = "no";

I planned on using this string to control a while loop in my code, and a switch within the while loop. The loop starts with: while(command = "no") and within the while loop, I use the Scanner I created earlier to get the user to define values for a String and an int, which when the code is executed, works. After that (but still within the while loop, I put command = input.nextLine();, followed by input.close(); on the line below, to set the value of the command string, and close the Scanner. But when I execute the code, it seems to skip that, and the switch which I put directly after that (which should also be controlled by the command string), and go on to code after the while loop, even though command is still set to "no", which should make the while loop start again.

How do I make the code pause and wait for something to be entered at command = input.nextLine();, and how do i make the switch work like it should?

Here is the entire code:

`import java.util.Scanner;

class Human {
    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        String name = this.name;
        return name;
    }

    public int getAge() {
        int age = this.age;
        return age;
    }
}

public class Asdfghjkl {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String command = "no";

        int age;
        String name;

        while(command == "no") {
            System.out.println("Welcome to HUMAN CREATOR");
            System.out.println("What is the human's name?");
            name = input.nextLine();
            System.out.println("Okay, its name is " + name + ".");

            System.out.println("What about its age?");
            age = input.nextInt();
            System.out.println(name + " is " + age + " years old.");
            System.out.println("Is this correct?");
            command = input.nextLine();

            switch (command) {
            case ("yes"):
                System.out.println("Great! Lets make the human!");
                Human h1 = new Human();
                h1.setAge(age);
                h1.setName(name);

            case ("no"):
                System.out.println("Okay, lets start again.");
            }
        }
        System.out.println("qwerty");
            input.close();
    }
}
user3308082
  • 127
  • 1
  • 10
  • Are you using java 6 or java 7? – newuser Apr 02 '14 at 02:42
  • @HovercraftFullOfEels that only answers half my question... how do i fix the issue of the `scanner` not working? and i tried `.equals` instead of `==` and it didnt seem to work – user3308082 Apr 02 '14 at 02:43
  • @newuser: it looks like it has to be 7, else it shouldn't compile. – Hovercraft Full Of Eels Apr 02 '14 at 02:43
  • @HovercraftFullOfEels that only answers half my question... how do i fix the issue of the `scanner` not working? and i tried `.equals` instead of `==` and it didnt seem to work – user3308082 Apr 02 '14 at 02:44
  • User: you appear to be closing the Scanner variable, input, ***inside*** of the while loop, before you're done using it which begs the question, why? Wouldn't it make more sense to close it after the while loop, so that it remains usable with each iteration of the loop? – Hovercraft Full Of Eels Apr 02 '14 at 02:44
  • I apologise if this is a duplicate of "how do i compare strings in java" but the part about the `while` not working was not the main reason i came here... im more interested in finding an answer for the `scanner` issue – user3308082 Apr 02 '14 at 02:47
  • @HovercraftFullOfEels I moved the closing of the scanner outside the `while` loop, and it made no difference when the code was executed – user3308082 Apr 02 '14 at 02:49
  • @user3308082 take a look of using nextLine after nextInt http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-nextint – newuser Apr 02 '14 at 02:51
  • @HovercraftFullOfEels i'm using java 8 – user3308082 Apr 02 '14 at 02:51
  • @user3308082 there are two answers to your question on why the .nextLine() is being skipped. – Stephen Buttolph Apr 02 '14 at 02:53
  • @newuser I looked at the link you just gave me, and it worked for making the `Scanner` wait for user input, but the switch still isnt working... I assume that would be fixed by looking at the "how do i compare strings in java" question mentioned earlier? – user3308082 Apr 02 '14 at 02:56
  • did you add the break; statement before the no case. – newuser Apr 02 '14 at 02:59
  • Now for some reason the `command.equals` is working, (it wasn't before) and the while loop is functioning as it should, but the `switch` is still not working properly. when the second `case` is true, it runs the code in the second `case`, but when the first `case` is true, it runs the code in both cases – user3308082 Apr 02 '14 at 03:03
  • @newuser I did not... i tried that and it worked. I feel like an idiot for forgetting the `break;`! hahaha. Thank you and everyone else who helped! – user3308082 Apr 02 '14 at 03:05
  • @user3308082 you are always welcome. – newuser Apr 02 '14 at 03:06

5 Answers5

2
while(command.equals("no"))

instead of

while(command == "no")

and also the case ("yes"): not having break; statement at the end, so it will continue the next case also.

Scanner#nextInt method does not read the last newline character of your input - here

Community
  • 1
  • 1
newuser
  • 7,892
  • 2
  • 23
  • 33
2

Change your:

age = input.nextInt();

To a:

age = Integer.parseInt(input.nextLine());

The reason "nextInt()" doesn't work correctly is because it leaves an empty space in the queue that gets picked out at the next "nextLine()"

Also you should always use .equals("some string") when comparing two strings.

I hope this fixes your bug :)

Also as "Hovercraft Full Of Eels" pointed out if you do need to repeat the while loop, the scanner will be closed and therefore will cause an error.

Stephen Buttolph
  • 623
  • 8
  • 16
0

Try this:while(command.equals("no"))

Benjamin
  • 2,169
  • 1
  • 12
  • 20
0

while(command == "no") won't work as you expect it too because your testing equality of a string. When doing this you need to use .equals which tests that the string value is equal.

so use:

while(command.equals("no"))

asdf
  • 585
  • 1
  • 8
  • 25
0

You also appear to be closing the Scanner variable, input, inside of the while loop, before you're done using it.

while(!command.equalsIgnoreCase("yes")) {
    System.out.println("Welcome to HUMAN CREATOR");
    System.out.println("What is the human's name?");
    name = input.nextLine();
    System.out.println("Okay, its name is " + name + ".");

    System.out.println("What about its age?");
    age = input.nextInt();
    System.out.println(name + " is " + age + " years old.");
    System.out.println("Is this correct?");
    command = input.nextLine();
    input.close();   // !! ************ here *****

    switch (command) {
    case ("yes"):
        System.out.println("Great! Lets make the human!");
        Human h1 = new Human();
        h1.setAge(age);
        h1.setName(name);

    case ("no"):
        System.out.println("Okay, lets start again.");
    }
}

Wouldn't it make more sense to close it after the while loop, so that it remains usable with each iteration of the loop?

Also add an extra input.nextLine(); after you get the input.nextInt() to capture the end of line token:

age = input.nextInt();
input.nextLine();
Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346