Results 1 to 3 of 3
- 01-10-2012, 12:44 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
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? :)
Java 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; } ...Last edited by Illanair; 01-10-2012 at 12:47 PM.
- 01-10-2012, 01:03 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 01-10-2012, 01:10 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
End Scanner int input with text value
By BillyB in forum New To JavaReplies: 3Last Post: 03-07-2011, 05:13 PM -
long to double trouble (Rhyme scheme not intended)
By sdeverteuil in forum New To JavaReplies: 2Last Post: 08-18-2009, 11:02 PM -
Scanner Skip
By right2001 in forum New To JavaReplies: 16Last Post: 04-17-2009, 03:08 AM -
Scanner input problem
By slayer_azure in forum New To JavaReplies: 3Last Post: 05-26-2008, 10:49 PM -
Error: convert from String to long
By bbq in forum New To JavaReplies: 1Last Post: 06-29-2007, 07:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks