0

因此,我目前正在開發一個將IEEE-754單精度和雙精度浮點數轉換爲十進制數的程序。該程序有一個拋出java.lang.NumberFormatException。我希望有人向我解釋爲什麼它會被拋出,以及我應該如何解決它。將IEEE-754雙精度和單精度轉換爲十進制Java bug

//This is the method being used for the IEEE-754 double-precision to decimal 
//line 5 is where the error is thrown 

1 double deciFinal; 
2 System.out.println("What IEEE-754 double precision floating-point representsation will you like to input?"); 
3 ieee754 = input.nextLine(); 
4 ieee754 = ieee754.trim(); 
5 deciFinal = Double.longBitsToDouble(Long.parseLong(ieee754,2)); 
6 System.out.println(deciFinal); 


//This is the method being used for the IEEE-754 single-precision to decimal 
//Line 5 is also where the error is being thrown. 

1 int binIeee; 
2 float deciFinal; 
3 System.out.println("What IEEE-754 single precision floating-point representsation will you like to input?"); 
4 ieee754 = input.nextLine(); 
5 deciFinal = Float.intBitsToFloat(Integer.parseInt(ieee754, 2)); 
6 System.out.println(deciFinal); 

這裏是我完整的代碼,如果你想引用它來幫助自己瞭解更多

import java.util.Scanner; 
/** 
* 
* @author Edwin 
*/ 
public class DecimalToIEE754 { 
    public static void main(String[]args){ 
    int choice; 
    Scanner input = new Scanner(System.in); 

    do{ 
     double deciNum; 
     String ieee754 = " "; 
     int bitsVal; 
     String bitsString; 
     System.out.println("Hello Welcome to the Decimal and IEEE-754 converter"); 
     System.out.println("Please select the number that correspondes with the conversion you will like:" 
       + "\n 1) Convert decimal number to IEEE-754 Single Precision Floating-Point Representation" 
       + "\n 2) Convert decimal number to IEEE-754 Double Precision Floating-Point Representation" 
       + "\n 3) Convert IEEE-754 Single Precision Floating-Point Representation to decimal number" 
       + "\n 4) Convert IEEE-754 Double Precision Floating-Point Representation to decimal number " 
       + "\n 0) Exit Converter"); 
     choice = input.nextInt(); 

     if(choice == 1) 
     { 
      System.out.println("What decimal number will you like to convert?"); 
      deciNum = input.nextDouble(); 
      float f = (float)deciNum; 
      bitsVal = Float.floatToIntBits(f); 
      bitsString = Integer.toBinaryString(bitsVal); 
      System.out.println(bitsString); 
     } 

     if(choice == 2) 
     { 
      System.out.println("What decimal number will you like to convert?"); 
      deciNum = input.nextDouble(); 
      bitsString = Long.toString(Double.doubleToLongBits(deciNum), 2); 
      System.out.println(bitsString); 
     } 

     if(choice == 3) 
     { 
      int binIeee; 
      float deciFinal; 
      System.out.println("What IEEE-754 single precision floating-point representsation will you like to input?"); 
      ieee754 = input.nextLine(); 
      **deciFinal = Float.intBitsToFloat(Integer.parseInt(ieee754, 2));** 
      System.out.println(deciFinal); 
     } 
     if(choice == 4) 
     { 
      double deciFinal; 
      System.out.println("What IEEE-754 double precision floating-point representsation will you like to input?"); 
      ieee754 = input.nextLine(); 
      ieee754 = ieee754.trim(); 
      **deciFinal = Double.longBitsToDouble(Long.parseLong(ieee754,2));** 
      System.out.println(deciFinal); 
     } 
    }while (choice != 0); 

} 
} 

一旦我輸入3或4 IEEE-754轉換爲十進制出現錯誤。它不允許我輸入Ieee-754號碼。完全錯誤是:

Exception in thread "main" java.lang.NumberFormatException: For input string: "" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Integer.parseInt(Integer.java:504) 
    at DecimalToIEE754.main(DecimalToIEE754.java:53) 
Java Result: 1 
+0

什麼是輸入,什麼是例外(完整的郵件),你所期望的輸出? – assylias

+0

這是說你傳遞了一個空字符串。打印'ieee754',它可能會證實... – assylias

+0

我不確定爲什麼會出現這種情況,因爲即使我手動輸入Ieee754號碼,它仍然會出現錯誤 –

回答

1

當你調用

Scanner.nextInt(); 

其次

Scanner.nextLine(); 

意味着nextLine()將數後讀取行的其餘部分。你可能沒有輸入任何數字,所以nextLine返回空的String「」,你可以在拋出的Exception中看到它。

解決這個問題的簡單方法是調用

int option = scanner.nextInt(); 
scanner.nextLine(); // ignore the rest of the line. 

// now reads the next line 
String line = scanner.nextLine(); 

最有可能你有一個負數。如果您有一個數字(最高位設爲1)10101010 ... 1010101並且長度爲32位,則這個數字太大而無法存儲在32位int型簽名的 int中。您可以將它解析爲Long並將其轉換爲(int)

您嘗試將64位二進制文​​件解析爲Long時存在同樣的問題。在這種情況下,您必須使用BigInteger並將其轉換爲長整型,或編寫自己的解析器。

+0

我沒有輸入號碼,當我這樣做時仍然有異常拋出我試圖你的方法解析爲一個長,並將其轉換爲一個整數,但它仍然拋出異常。 –

+0

@EdwinLobo這是在哪裏閱讀你拋出的異常小心幫助;) –

0

你的問題是在這裏:choice = input.nextInt();

nextInt消耗的int,而不是換行字符。所以,下次你打電話時nextLine您會收到一個空字符串,因爲上線的一切已經被消耗=>你需要添加一個nextLine

choice = input.nextInt(); 
nextLine(); 

//go on with your code 

同樣適用於nextDouble

參見:Scanner issue when using nextLine after nextXXX

+0

正如我已經提到的問題是與ieee754 = nextLine();與choice = input.nextInt()無關;因爲這涉及到人們想要進行哪種類型的轉換。 –

+0

@EdwinLobo是和否 - 如果你在'nextInt'之後加入'nextLine',並且留下你的其他代碼,它也應該可以工作。 – assylias

相關問題