本文介绍了“线程中的例外”主要“ java.util.InputMismatchException" **的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了但我似乎无法在代码中找到任何错误,请帮助!

I have searched but i really can' t seem to find anything wrong in the code, please help!

代码编译但是,这是我得到的错误当我想回答问题3时:

The code compiles but, this is the error i get when i want to answer question 3:

Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Unknown Source)
        at java.util.Scanner.next(Unknown Source)
        at java.util.Scanner.nextDouble(Unknown Source)
        at ForgetfulMachine.main(ForgetfulMachine.java:16)

这是我的代码:

import java.util.Scanner;

public class ForgetfulMachine
{
    public static void main( String[] args )
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println( "What city is the capital of Germany?" );
        keyboard.next();

        System.out.println( "What is 6 divided by 2?" );
        keyboard.nextInt();

        System.out.println( "What is your favorite number between 0.0 and 1.0?" );
        keyboard.nextDouble();

        System.out.println( "Is there anything else you would like to tell me?" );
        keyboard.next();
    }
}


推荐答案

Scanner 将抛出此异常。特别是,在您的情况下,如果使用错误的小数分隔符。 都是常见的特定于语言环境的小数分隔符。

Scanner will throw this exception if the entry is in a format that is incorrect for the Scanner's Locale. Particularly, in your case, if the wrong decimal separator is used. Both . and , are common locale-specific decimal separators.

要找出您的默认语言环境的小数点分隔符,您可以使用:

To find out what the decimal separator is for your default locale you may use:

System.out.println(
    javax.text.DecimalFormatSymbols.getInstance().getDecimalSeparator()
);

参见:





  • Scanner#locale()
  • Scanner#useLocale(Locale)
  • DecimalFormatSymbols#getInstance(Locale)

这篇关于“线程中的例外”主要“ java.util.InputMismatchException" **的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:26