Java 扫描器:“0”不是双

发布于 2024-12-29 09:44:56 字数 2470 浏览 1 评论 0 原文

我的java扫描仪有问题:我正在尝试使用此代码读取双精度数:

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

但是当要读取的数字是“0.0”或“0”时。或“0”我有以下错误:

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

这是“nextDouble()”函数的正常行为吗?

在没有这个问题的情况下读取双精度数的最简单方法是什么?

我真的需要捕获错误然后使用“netxInt()”重试吗?

感谢您的帮助。


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

它在 javac 上工作正常,但在 gcj (在最新的 ubuntu 上)上它不起作用。底部提供了 gcj 版本的详细信息。


Full code :

import java.util.Scanner;

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

输入(标准输入):

0.0

输出:

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) 

其他服务器上的“gcc 版本 4.4.5”上的行为相同。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚气少女 2025-01-05 09:44:56

我看到 gcj 也有同样的问题。并使用其他输入,行为非常奇怪 - 空格没有被跳过,并且 1 也不是双精度:

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

所以我抓住了 gnu classpath 0.99 并查看了代码。以下似乎被称为:

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);

我已经简化/清理了一点。

这似乎只是接受转换为字符串时与源完全匹配的值。这解释了为什么拒绝前导空格和没有小数点的值。

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.

回眸一遍 2025-01-05 09:44:56

对我来说效果很好(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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文