2016-11-26 25 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的實例。

祝你好運!

相關問題