java - "main"java.util.InputMismatchException

标签 java

每当我尝试编译它时,它总是给我一个异常: enter image description here

这是我的源代码:

Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.##");

System.out.print("Gaji Pokok (x 10.000) : ");
double gaji = input.nextDouble();

System.out.print("Lama Tahun Kerja : ");
int th = input.nextInt();

System.out.print("Lama Bulan Kerja : ");
float bl = input.nextFloat();

if ( bl > 12)
{
    System.out.println("Inputan bulan anda salah");
    System.out.print("Masukkan kembali bulan yang benar : ");
    float blnnew = input.nextFloat();
    float tukar = blnnew;
    bl = tukar;
}
float fak_peng;
fak_peng = Float.valueOf(df.format((th+(bl/12))*2.5));

System.out.print("Jumlah Faktor Penghargaan :  " );
System.out.println(fak_peng + " %");

System.out.println("Nilai Sekarang : 1.0000000 " );


float per_mppeg;
per_mppeg = Float.valueOf(df.format(gaji*(fak_peng/100)*1));
System.out.print("Perhitungan MP Pegawai :  " );

System.out.println(gaji + " x " + fak_peng + "% x " + " 1.0000000 = Rp." + (per_mppeg) + "(x 10.000)");

System.out.print("MP Perbulan :  " );
System.out.println(per_mppeg + " + 100% = Rp." + (per_mppeg) + "(x 10.000)");

System.out.println("MP sekaligus 100% : ");


float peserta;
peserta = Float.valueOf(df.format(100.6650*per_mppeg));
float jd;
jd = Float.valueOf(df.format(14.4820*per_mppeg*0.8));
float anak;
anak = Float.valueOf(df.format(0.6090*per_mppeg*0.8));
float jml;
jml = Float.valueOf(df.format(peserta+jd+anak));
System.out.println("   Peserta = 100.6650 x "+ per_mppeg + "       = " + peserta + "(x 10.000)");
System.out.println("   Jd/Dd   =  14.4820 x "+ per_mppeg + " x 80% = " + jd + "(x 10.000)" );
System.out.println("   Anak    =   0.6090 x "+ per_mppeg + " x 80% = " + anak + "(x 10.000)");
System.out.println("Jumlah Total = "+ jml);

float mpdua;
mpdua = Float.valueOf(df.format (jml*0.2)) ;
float mpdel;
mpdel = Float.valueOf(df.format(per_mppeg*0.8)) ;
System.out.println("MP Sekaligus 20% = "+ mpdua + "(x 10.000)");
System.out.println("MP sekaligus 80% = "+ mpdel + "(x 10.000)");

最佳答案

您的异常不是编译时错误/异常;这是一个运行时异常。它被抛出是因为扫描仪正在读取的内容无法转换为您要求的类型(例如,扫描仪应该读取的下一个内容是“hello”,但您正在使用 scanner.nextInt() ,因为“hello”无法转换为整数,它将引发 InputMismatchException)。

在您的情况下,要求双倍时会引发异常。可能您使用了错误的语法。您应该检查您的系统使用哪种语法来表示 double 。例如,在某些系统上, double 的小数部分和整数部分应使用 , 分隔,而在其他系统上应使用 . 分隔。因此,第一种类型的系统上的一半应写为 0,5,但在第二种类型的系统上应写为 0.5。 在 Java 中,扫描器使用的语法是通过 Locale 实例定义的。 您可以使用 locale() 方法检查您的扫描仪使用哪一个,并使用 useLocale() 方法更改它。

因此您应该重新检查您提供的输入内容。

除了 double 格式的问题您正在以不推荐的方式创建 DecimalFormat(请参阅下面的最后一个引用)并且还有另一行可能会引发异常( NumberFormatException ),如果您不注意您正在使用的 Locale 实例:

fak_peng = Float.valueOf(df.format((th+(bl/12))*2.5));

当您使用自己的格式解析小数时(new DecimalFormat("#.##");),该字符串将传递给Float.valueOf 方法将取决于用于创建 DecimalFormat 对象 dfLocale 实例(在代码示例中,您没有使用特定的 Locale 实例,以便使用系统默认的 Locale 实例)。 但是 Float.valueOf 希望其参数使用 Java™ 语言规范 定义的特定语法,无论您的系统如何,如 Java API for Float.valueOf 中所写:

[...] where Sign, FloatingPointLiteral, HexNumeral, HexDigits, SignedInteger and FloatTypeSuffix are as defined in the lexical structure sections of The Java™ Language Specification, except that underscores are not accepted between digits. If s does not have the form of a FloatValue, then a NumberFormatException is thrown.

(完整的文本太大,无法包含在此处。请按照 this link 或上面的内容获取有关 Sign、FloatingPointLiteral、HexNumeral、HexDigits、SignedInteger、FloatTypeSuffix 和 FloatValue 确切表示的更多信息)

如果您想更改 DecimalFormat 对象中使用的 Locale 实例,请读取 the API for the DecimalFormat class

To obtain a NumberFormat for a specific locale, including the default locale, call one of NumberFormat's factory methods, such as getInstance(). In general, do not call the DecimalFormat constructors directly, since the NumberFormat factory methods may return subclasses other than DecimalFormat.

在 API 中(点击引用之前的链接),他们给出了一个示例,说明如何正确创建 NumberFormat 的实例。

祝你好运!

关于java - "main"java.util.InputMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40818873/

相关文章:

java - NumberFormat 数字后显示的文本/货币

java - 有什么方法可以判断方法是否结束或本地方法不再使用?

java - 如何在多个实体中使用通用实体

java - 启用 jmxremote 的 JBoss 4.2.2 上的 JBossWS 中的 NPE

java - 如何从逗号拼接的数据库字符串中进行 SELECT - SQL

java - Eclipse:构造函数未定义

java - 使用 gradle 3.0.0-alpha2 时 Android Studio 3.0 Canary 2 : Failed to apply plugin,

java - Tomcat启动失败,服务器8080端口已被占用

java - 如何编码 URI 参数值?

java - myLooper() 在异步任务期间不会退出