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