Results 1 to 3 of 3
  1. #1
    Illanair is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Unhappy 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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default 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.

  3. #3
    Illanair is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default 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.

Similar Threads

  1. End Scanner int input with text value
    By BillyB in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 05:13 PM
  2. long to double trouble (Rhyme scheme not intended)
    By sdeverteuil in forum New To Java
    Replies: 2
    Last Post: 08-18-2009, 11:02 PM
  3. Scanner Skip
    By right2001 in forum New To Java
    Replies: 16
    Last Post: 04-17-2009, 03:08 AM
  4. Scanner input problem
    By slayer_azure in forum New To Java
    Replies: 3
    Last Post: 05-26-2008, 10:49 PM
  5. Error: convert from String to long
    By bbq in forum New To Java
    Replies: 1
    Last Post: 06-29-2007, 07:23 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •