1

I have a project the is suppose add an input to an array list. I used scanner.util for my input, I'm suppose to input a Title = (String) and ID = (int), after input the project then provides me an option to input another or exit, for this i used while loop that when the input value is not equal to "Exit", loop. The problem is my project skips the input option to Exit or continue, does anyone have any idea how to fix it???

here is the code

String option = "";
while(!option.equals("Exit"))
{
    System.out.println("Add your New Movie below\n");

    movie.setName(input.nextLine());
    movie.setid(input.nextInt());

    System.out.println("Type in 'Quit'to end, type anything to add another Movie");

    movieList.add(movie.getid() + " " + movie.getName());

    option = input.nextLine();
}   
Colin Basnett
  • 3,735
  • 1
  • 27
  • 46
  • 1
    you are asking the user to input Quit to end , and trying to compare Exit. – Satya May 19 '14 at 03:37
  • Make sure you are typing "Exit" case sensitive. Other than that it looks like it should work. But as Satya said you are telling them to type quit but comparing exit. Maybe you should use `while(!option.equalsIgnoreCase("Exit"))` so you dont have case comparison issues. – ug_ May 19 '14 at 03:48

2 Answers2

2

Well, the problem is pretty obvious, the methods nextXXX in java.util.Scanner will only read the value of type XXX.

This means your return character "\n" is still available for read, so the input Exit is not even tested the option is always tested against a "\n".

I suggested using a better InputStream reader, or BufferedReader or anything else. If you still want go with this code then add another option.nextInt(); at the end of this loop.

String option = "";
while(!option.equals("Exit"))
{
    System.out.println("Add your New Movie below\n");

    movie.setName(input.nextLine());
    movie.setid(input.nextInt());

    System.out.println("Type in 'Quit'to end, type anything to add another Movie");

    movieList.add(movie.getid() + " " + movie.getName());

    option = input.nextLine();  
    option = input.nextLine();  
}   
dharam
  • 7,306
  • 15
  • 59
  • 87
  • 1
    Maybe just use `movie.setid(Integer.parseInt(input.nextLine()));` instead of the double readline, that way your code stays a bit more clear. – ug_ May 19 '14 at 03:59
1

nextInt() only reads the next int from the input stream, and leaves any other characters on the stream.

When you press "enter" after entering an ID, your computer sticks either a newline character ('\n') on *nix systems or a newline and a carriage return ("\r\n") on Windows systems at the end of the line. So this is what happens on a *nix system when you enter the ID:

User input:
> 5\n
Process next int: return 5
// So now "\n" is left on the input stream

// Code goes to option = input.nextLine()
// "\n" is still on the input stream, so nextLine() immediately returns an empty string

You should read the entire line when getting the next integer and use Integer.parseInt() to extract its value.

awksp
  • 11,292
  • 4
  • 35
  • 44