Results 1 to 5 of 5
  1. #1
    Otto45 is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default how to test for certain input format

    I'm trying to test if the next line of input from the standard input has the format:
    integer integer double
    with the code:
    Java Code:
    Scanner scanner = new Scanner(System.in);
    String line;
    while ((line = scanner.nextLine()).matches("\\d+ \\d+ \\p{Digit}")) {
        //do stuff
    }
    However, my while loop never executes. Am I testing for two integers and a double per line correctly?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: how to test for certain input format

    If I have to believe the regex documentation:

    Pattern (Java Platform SE 7 )

    Then \p{Digit} and \d are basically the same thing. And no, a single digit does not match an entire double value.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,619
    Rep Power
    5

    Default Re: how to test for certain input format

    No. You are checking for an int, int, digit. I would recommend that you simply read in a whole line and attempt to parse it using the
    the built in parse methods of the Integer and Double classes. The methods will throw exceptions for incorrectly formatted tokens.
    The String class might also be of use. You could also just use nextInt() and nextDouble() and catch the InputMismatchException.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: how to test for certain input format

    Or call hasNextInt etc methods.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to test for certain input format

    Testing for a possible int value with a regular expression \d+ is extremely dangerous because a sequence of, say, 80 digits matches the pattern but it is most definitely too large to fit in an int and Integer.parseInt( ... ) will throw an Exception.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 02-09-2012, 09:25 AM
  2. input masking 24hour time format (hh:nn) to jTextField
    By newbiejava in forum New To Java
    Replies: 2
    Last Post: 07-24-2010, 01:12 PM
  3. Replies: 0
    Last Post: 03-17-2009, 07:40 AM
  4. validate time input format
    By Alairmon in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 07:39 AM

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
  •