因此,我目前正在进行一个程序,它将把IEEE-754单精度和双精度浮点转换为十进制数。这个程序有一个java.lang.NumberFormatException抛给它。我希望有人能向我解释为什么要扔它,以及我应该如何去修复它。
//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);
}
}
一旦输入Ieee-754的3或4以转换为十进制,错误就会出现.它不允许我输入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
发布于 2014-02-13 08:54:16
当你打电话的时候
Scanner.nextInt();
紧接着是
Scanner.nextLine();
这意味着nextLine()
将在数字之后读取行的其余部分。您可能没有在数字之后输入任何内容,因此nextLine返回空字符串"“,您可以在抛出的异常中看到这一点。
解决这个问题的简单方法就是打电话
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)
您在尝试解析64位二进制文件时遇到了与Long相同的问题。在这种情况下,您必须使用BigInteger并将其转换为long,或者编写自己的解析器。
发布于 2014-02-13 17:11:10
你的问题在这里:choice = input.nextInt();
nextInt
使用一个int
,但不使用linefeed字符。因此,下次调用nextLine
时,会收到一个空字符串,因为行中的所有内容都已被消耗,所以需要添加一个nextLine
。
choice = input.nextInt();
nextLine();
//go on with your code
nextDouble
也是如此。
https://stackoverflow.com/questions/21749101
复制相似问题