Scanner.Next(Long/String/Double) - How do I skip the input?
Heya.
I'm fairly new to Java, and I have a question regarding the scanner.
Basically I'm using the Scanner.Next (String/Long/Double) to get user input via the console for storage in variables. You know - like adding a recipe to a cookbook kind of thing.
Thing is - I use the same method for changing the saved content. Problem is that I can't get the scanners to accept a blank response (aka. enter with no input).
I kind of need this as the program promts the user for all fields one by one, and a blank enter should signify "skip this and leave the old information". (That last I've got covered - I just need a way of letting the Scanner/method to accept a blank reponse and move on to the next line of code so I can return the old value)
Does anyone know a way around this problem? :)
Code:
...
private long inputNr()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("\fType Nr:" );
long title = keyboard.nextString();
return title;
}
private String inputType()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("\Type type:" );
String title = keyboard.nextLine();
return title;
}
private double inputPrice()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("\fType price:" );
double title = keyboard.nextDouble();
return title;
}
...
Re: Scanner.Next(Long/String/Double) - How do I skip the input?
From the API:
"
The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt()) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext and next methods may block waiting for further input.
"
Since carriage return is whitespace, and that is the default definition of a delimiter for Scanner, all bar nextLine() (and possibly nextString()?) wil block until something is entered.
What you want to do is do nextLine() for all input and then convert it as needed. That will allow you to handle null entries.
Re: Scanner.Next(Long/String/Double) - How do I skip the input?
That makes sense - I had a look at the API, but I'm not quite used to advanced english so it didn't make much sense. (or rather technical english).
I'll give nextline and convert the input to the types I need, thanks.