Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将IEEE-754双精度和单精度转换为十进制Java错误_Java_Error Handling_Floating Point_Ieee 754_Numberformatexception - Fatal编程技术网

将IEEE-754双精度和单精度转换为十进制Java错误

将IEEE-754双精度和单精度转换为十进制Java错误,java,error-handling,floating-point,ieee-754,numberformatexception,Java,Error Handling,Floating Point,Ieee 754,Numberformatexception,因此,我目前正在开发一个程序,将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 S

因此,我目前正在开发一个程序,将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);

}
}
当我输入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
当你打电话的时候

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中。您可以将其解析为long,并将其转换为
(int)


尝试将64位二进制文件解析为长字符串时也会遇到同样的问题。在这种情况下,您必须使用BigInteger并将其转换为long,或者编写自己的解析器。

您的问题就在这里:
choice=input.nextInt()

nextInt
使用
int
,但不使用换行符。因此,下次调用
nextLine
时,您将收到一个空字符串,因为该行中的所有内容都已被使用=>您需要添加一个
nextLine

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

//go on with your code
这同样适用于
nextDouble


另请参见:

输入是什么,异常是什么(完整消息),您期望的输出是什么?它表示您传递了一个空字符串。打印
ieee754
,它可能会确认……我不确定为什么会这样,因为即使我手动输入ieee754编号,它仍然会出现错误replace
ieee754=input.nextLine()带有
ieee754=“100”
并查看它是否有效。如果您知道问题出在
input.nextLine()
没有按照您的预期进行操作。是的,这就是我刚刚更改input.nextLine()的问题输入。下一步,现在它工作得很好谢谢。我没有输入的数字,当我输入时仍然抛出异常,我尝试了你的方法将其解析为long并将其转换为int,但它仍然抛出异常。@EdwinLobo这是仔细阅读抛出的异常的帮助;)正如我已经提到的,问题在于ieee754=nextLine();与choice=input.nextInt()无关;因为这涉及要进行哪种类型的转换。@EdwinLobo Yes和no-如果在
nextInt
之后包含
nextLine
,并保留其余的代码,它也应该可以工作。