2

I have a problem with the java Scanner : I'm trying to read doubles using this code :

Scanner sc = new Scanner(System.in);
double value = sc.nextDouble();

However when the number to read is "0.0" or "0." or "0" I have the following error :

java.util.InputMismatchException: "0" is not a double

Is that the normal behaviour of the "nextDouble()" function ?

What's the easiest way to read a double without having this problem ?

Do I really need to catch the error and then try again with "netxInt()" ?

Thanks for your help.


For information I'm using the java compiler "gcj".

It's working fine with javac but with gcj (on an up-to-date ubuntu) it's not working. Details of the gcj version are provided at the bottom.


Full code :
import java.util.Scanner;

class Main
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      double f = sc.nextDouble();
   }
}

Input (on standard input) :

0.0

Output :

Exception in thread "main" java.util.InputMismatchException: "0" is not a double
*** Got java.lang.OutOfMemoryError: Cannot create additional threads while trying to print stack trace.


Result of a "gcj -v" :
COLLECT_GCC=gcj
COLLECT_LTO_WRAPPER=/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.5.2-8ubuntu1' --with-bugurl=file:///usr/share/doc/gcj-4.5/README.Bugs --enable-languages=c,c++,java --prefix=/usr --program-suffix=-4.5 --enable-shared --enable-multiarch --with-multiarch-defaults=i386-linux-gnu --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib/i386-linux-gnu --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.5 --libdir=/usr/lib/i386-linux-gnu --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-time=yes --disable-libmudflap --enable-plugin --enable-gold --enable-ld=default --with-plugin-ld=ld.gold --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.5/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.5 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.5 --with-arch-directory=i386 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu1) 

Same behaviour on a "gcc version 4.4.5" on an other server.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Loïc Février
  • 7,540
  • 8
  • 39
  • 51
  • 1
    It works fine for me on `javac` compiler. – RanRag Jan 29 '12 at 19:05
  • 1
    From what I glean from the docs this shouldn't happen and might just be a bug in your class libraries. – Joey Jan 29 '12 at 19:05
  • It works fine for me. Are you sure that the text doesn't contain the letter O not the number 0? You've either got a but in your code or a misunderstanding of your data. Please post an exact copy of the offending line of text and the offending code. – Hovercraft Full Of Eels Jan 29 '12 at 19:05
  • The specification for `Scanner.nextDouble()` says it should accept the Float regex in http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#number-syntax, which should match "0" just fine...that's weird. – Louis Wasserman Jan 29 '12 at 19:05
  • I don't get this behaviour. It works fine for me. Are the quotes part of the input, perhaps you need to remove them first? – Robert Jan 29 '12 at 19:06
  • Even with quotes i can't replicate that error `Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextDouble(Scanner.java:2456) at demo.main(demo.java:7)` – Insidi0us Jan 29 '12 at 19:08
  • I confirm that the quotes are not in the input and it's really is "0" and not "O". – Loïc Février Jan 29 '12 at 19:12
  • Can you reproduce the problem using a 'standard' JDK (Oracle, OpenJDK, IBM)? – home Jan 29 '12 at 19:28
  • 1
    The question was "how to fix this with the gcj compiler, that it works on the javac is not a surprise :) – Peter Svensson Jan 29 '12 at 19:30
  • 1
    Yes, the answer is to not use the gcj compiler! – Hovercraft Full Of Eels Jan 29 '12 at 19:50
  • @Hovercraft Full Of Eels : well that's not an option for me ;) – Loïc Février Jan 29 '12 at 19:52
  • Q: So why is it a requirement to use gjc? Q: Have you considered just writing your own "mini scanner" (e.g. "String.subString()", "Double.parseDouble()", etc as a workaround for this apparent gjc bug? Q: How quickly can you *migrate* from gjc? ;) – paulsm4 Jan 29 '12 at 21:02
  • @paulsm4 : It's for a automatic evaluation system of programming exercises. The JVM was not working well inside the sandbox used to control memory/time constraints. Yes I can write my own Scanner, I ask this question to see if it was or not a bug and if someone knew about this. If it's indeed a bug, I will report it. – Loïc Février Jan 29 '12 at 21:06

2 Answers2

2

i see the same problem with gcj. and playing around with other inputs, the behaviour is very odd - space is not skipped and 1 is not a double either:

> ./a.out
1
Exception in thread "main" java.util.InputMismatchException: "1" is not a double
   at java.util.Scanner.myNextDouble(libgcj.so.12)
   at java.util.Scanner.nextDouble(libgcj.so.12)
   at Main.main(a.out)
> ./a.out
1.
Exception in thread "main" java.util.InputMismatchException: "1" is not a double
   at java.util.Scanner.myNextDouble(libgcj.so.12)
   at java.util.Scanner.nextDouble(libgcj.so.12)
   at Main.main(a.out)
> ./a.out
1.1
> gcj --version
gcj (SUSE Linux) 4.6.2

so i grabbed the source for gnu classpath 0.99 and had a look at the code. the following is what seems to be called:

String tmp;
double rc;
try {
    tmp = myApplyLocale(tmp, 10);
    rc = Double.parseDouble(tmp);
    if (("" + rc).equals(tmp)) {
        return rc;
    }
}
catch (ParseException e) {}
throw new InputMismatchException(ERR_PREFIX + tmp + NOT_DOUBLE);

where i've simplified/cleaned a little.

and what that seems to be doing is only accepting values that, when converted to a string, exactly match the source. which explains why leading spaces and values without decimal points are rejected.

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
  • So we have an explanation, but it still seems odd.. Should I try to submit a bug report so that gcj will act as javac does ? – Loïc Février May 19 '12 at 13:50
  • @LoïcFévrier well, it looks like the behaviour is inconsistent with the JAVA API at http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextDouble() - in particular, that refers to the float regexp (defined higher in the page), which matches integers. so yes, it seems like a bug. you can file reports at http://www.gnu.org/software/classpath/bugs.html and a quick search http://gcc.gnu.org/bugzilla/buglist.cgi?quicksearch=scanner suggests it doesn't already exist. on the other hand, given the current oracle lawsuit, maybe they cannot program against the api... – andrew cooke May 19 '12 at 16:46
1

Works fine for me (javac 1.6):

Scanner sc = new Scanner("0,1 0, 0");
System.out.print("" + sc.nextDouble() + " " + sc.nextDouble() + " " + sc.nextDouble()); // 0.1 0.0 0.0
msi
  • 2,609
  • 18
  • 20