2016-11-26 47 views
0

每当我尝试编译它,它不断给我一个例外: enter image description here“主” java.util.InputMismatchException

这是我的源代码:

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)"); 
+0

您必须注意:我看到一些可能会引起示例代码中的另一个异常的东西。请阅读我的答案 –

回答

2

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

在你的情况下,当请求双倍时引发异常。可能你正在使用错误的语法。你应该检查你的系统使用哪种语法来表示双打。例如,在某些系统上,双精度的小数部分和整数部分应该用,分开,而在其他系统上用.分开。因此,第一类系统的一半应写为0,5,而第二类应写为0.5。 在Java中,扫描程序使用的语法是使用Locale实例定义的。 您可以使用locale()方法检查您的扫描仪使用的是哪一个,并使用useLocale()方法对其进行更改。

所以你应该重新检查你给出的输入。

除了你的问题的双重你在一个discommanded方式创建DecimalFormat的格式(见下面的最后报价)还有另一个可能上升异常(NumberFormatException的)线,如果你不注意你正在使用Locale实例:

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

你一个重新使用你自己的格式来解析十进制(new DecimalFormat("#.##");)将传递给Float.valueOf方法的字符串取决于用于创建DecimalFormat对象的Locale实例df(在代码示例中,您并未使用特定的Locale实例您的系统默认使用Locale实例)。 Float.valueOf预计它的参数使用由定义的特定语法的Java™语言规范无论你的系统如写在Java API for Float.valueOf

[...]其中注册,FloatingPointLiteral,HexNumeral,HexDigits ,SignedInteger和FloatTypeSuffix在The Java™Language Specification的词汇结构部分中定义,除了在数字之间不接受下划线。如果s不具有FloatValue的形式,则抛出NumberFormatException。

(完整的文字太大也包括在这里。按照this link或一个以上有什么注册,FloatingPointLiteral,HexNumeral,HexDigits,SignedInteger,FloatTypeSuffix和的floatValue完全代表详细信息)

如果你想改变你DecimalFormat对象使用的Locale例如,读the API for the DecimalFormat class

要获取特定语言环境(包括缺省语言环境)的NumberFormat,请调用NumberFormat的工厂方法之一,如getInstance()。通常,不要直接调用DecimalFormat构造函数,因为NumberFormat工厂方法可能会返回DecimalFormat以外的子类。

在API中(请参考引用之前的链接),他们举例说明了如何正确创建NumberFormat的实例。

祝你好运!

相关问题