Results 1 to 8 of 8
  1. #1
    Spike_CT is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Exclamation Scanner.nextFloat()

    Hi all,

    I am working on a scanner that can parse an Wavefront Obj File
    Problem is that it does not identify the floats in the obj-file correctly.

    I could regenerate my problem with this simple code snippet:

    Java Code:
    String text = "v -0.1304 -1.2910 0.2474";
    
    
    Scanner docScan = new Scanner(text);
    
    System.out.println(docScan.next());
    System.out.println(docScan.nextFloat());
    A text with the format [string] [float] [float] [float] could succesfully
    parse the first "v" but does not recognize the "-0.1304" as a float.

    Java Code:
     docScan.hasNextFloat());
    returns false
    and
    Java Code:
     docScan.nextFloat());
    crashes with a java.util.InputMismatchException

    The strange thing is that:

    Java Code:
    String text = "v -0.1304 -1.2910 0.2474";
    
    
    Scanner docScan = new Scanner(text);
    
    System.out.println(docScan.next());
    String floatNumber = docScan.next());
    
    System.out.println(Float.parseFloat(floatNumber));
    does work, while javadoc states that nextFloat() uses parseFloat internally

    I could mis something, but why does a docScan.nextFloat() generate an mismatch error on "-0.1304"?
    Last edited by Spike_CT; 01-27-2010 at 04:19 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Perhaps it's because "-0.1304" isn't a String representation of a float literal (a guess) since there's no "f" appended to the end. What if you use nextDouble() instead of nextFloat()?

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I got your code to work okay as given.
    Java Code:
    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            String text = "v -0.1304 -1.2910 0.2474";
            Scanner docScan = new Scanner(text);
            System.out.println(docScan.next());
            System.out.println(docScan.nextFloat());
            System.out.println(docScan.nextFloat());
            System.out.println(docScan.nextFloat());
    
            // Each of these worked okay by themselves in
            // place of the three println statements above.
    /*
            while(docScan.hasNextFloat()) {
                System.out.println(docScan.nextFloat());
            }
    
            while(docScan.hasNextDouble()) {
                System.out.println(docScan.nextDouble());
            }
    */
        }
    }
    Console output:
    Java Code:
    C:\jexp>javac test.java
    
    C:\jexp>java Test
    v
    -0.1304
    -1.291
    0.2474
    in
    Java Code:
    C:\jexp>java -fullversion
    java full version "1.6.0_17-b04"

  4. #4
    Spike_CT is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    I tried placing a f behind it, but it doesn't identify as a float either.
    I also tried 1f and 1.0f as input.

    Javadoc states that a decimal can be parsed to a float:

    Float :: = Decimal
    | HexFloat
    | SignedNonNumber

    A float is eather a decimal, a hexfloat or a signed NAN.

    from:

    Scanner (Java 2 Platform SE 5.0) (section: number syntax)

  5. #5
    Spike_CT is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    HardWired I copied your example program, compiled it, and run it in NetBeans.

    My java version matches your's:

    Java Code:
    C:\Users\Mike>java -fullversion
    java full version "1.6.0_17-b04"
    The resulting error message is:

    Java Code:
    v
    Exception in thread "main" java.util.InputMismatchException
            at java.util.Scanner.throwFor(Scanner.java:840)
            at java.util.Scanner.next(Scanner.java:1461)
            at java.util.Scanner.nextFloat(Scanner.java:2319)
            at javaapplication1.Main.main(Main.java:23)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    Main.main(Main.java:23) is the first nextFloat() call:

    Java Code:
    System.out.println(docScan.nextFloat());

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Then I would fall back to what you discovered earlier
    Java Code:
    System.out.println(Float.parseFloat(docScan.next()));

  7. #7
    Spike_CT is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    I found the problem:

    In the US a typical decimal is written like:

    -0.1304

    But in some other parts of the world, including my home country (The Netherlands) a decimal is written as:

    -0,1304

    My scanner was automatically initialized with a locale Locale.nl_NL and thus refused a "-0.1304" as a valid decimal.

    The following couldn't be parsed:

    Java Code:
    "String text = v -0.1304 -1.2910 0.2474";
    While the following could:

    Java Code:
    "String text = v -0,1304 -1,2910 0,2474";
    The problem was solved by configuring the scanner to an "American Scanner":

    Java Code:
    docScan.useLocale(Locale.US);

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

Similar Threads

  1. Help With Scanner
    By jtmoney0511 in forum New To Java
    Replies: 10
    Last Post: 10-13-2009, 12:24 AM
  2. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 03:11 PM
  3. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 04:37 PM
  4. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 02:41 PM
  5. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:00 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
  •