简体   繁体   English

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

[英]Converting IEEE-754 double and single precision to decimal Java bug

So I am currently working on a program that will convert IEEE-754 single and double-precision floating point into a decimal number. 因此,我目前正在研究将IEEE-754单精度和双精度浮点转换为十进制数的程序。 The program has a java.lang.NumberFormatException thrown at it. 该程序将引发java.lang.NumberFormatException I would like for someone to explain to me why it is being thrown and how I should go about fixing it. 我希望有人向我解释为什么会抛出它,以及我应该如何解决它。

//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);

Here is my complete code if you would like to reference it to help myself understand more 如果您想参考它,这是我的完整代码,以帮助自己了解更多

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);

}
}

The error appears once I input 3 or 4 for Ieee-754 to convert to decimal. 一旦输入3或4以便Ieee-754转换为十进制,就会出现错误。 It does not let me enter an Ieee-754 number. 它不允许我输入Ieee-754号码。 The error in full is: 完整的错误是:

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

When you call 你打电话时

Scanner.nextInt();

followed by 其次是

Scanner.nextLine();

means the nextLine() will read the rest of the line after the number. 表示nextLine()将读取数字后的其余行。 You might not have entered anything after the number so nextLine returns empty String "" which you can see in your Exception thrown. 您可能未在数字后面输入任何内容,因此nextLine返回空字符串“”,您可以在抛出的Exception中看到。

The simple way around this is to call 解决此问题的简单方法是致电

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

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

Most likely you have a negative number. 您极有可能是负数。 If you have a number which is (top bit is set 1) 10101010 ... 1010101 and is 32-bits long, this is too large to store in a 32-bit signed int. 如果您的数字是(最高位设置为1)10101010 ... 1010101,且长度为32位,则该数字太大,无法存储在32位带符号的 int中。 You can parse it as a Long and cast it to an (int) 您可以将其解析为Long并将其转换为(int)

You have the same problem with trying to parse a 64-bit binary as a Long. 尝试将64位二进制文​​件解析为Long时,您会遇到相同的问题。 In this case you have to use a BigInteger and cast this to a long, or write your own parser. 在这种情况下,您必须使用BigInteger并将其强制转换为long或编写自己的解析器。

Your problem is here: choice = input.nextInt(); 您的问题在这里: choice = input.nextInt();

nextInt consumes an int , but not the linefeed character. nextInt使用int ,但不使用换行符。 So the next time you call nextLine you receive an empty string because everything on the line has already been consumed => you need to add a nextLine : 因此,下次调用nextLine您会收到一个空字符串,因为该行上的所有内容都已被消耗掉=>您需要添加nextLine

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

//go on with your code

Same applies to nextDouble . 同样适用于nextDouble

See also: Scanner issue when using nextLine after nextXXX 另请参阅: nextXXX之后使用nextLine时的扫描仪问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java-将十六进制转换为IEEE-754 64位浮点数-双精度 - Java - Convert hex to IEEE-754 64-bit float - double precision 如何根据 IEEE-754 格式将十六进制值转换为单精度浮点数以及双精度浮点数 - How to convert hex value to single precision floating point number and also in double precision floating point number according to the IEEE-754 format Java浮动为IEEE-754十六进制? - Java float to IEEE-754 hexadecimal? 如何在 Java 中将 IEEE-754 二进制表示字符串转换为浮点数或双精度数? - How can I convert an IEEE-754 binary representation String to a float or double in Java? IEEE 754 双精度数从 MIPS C 到 Java 的转换 - IEEE 754 double-precision numbers from MIPS C to Java conversion 如何将Java double转换为byte [],如何将byte []转换为double(IEEE 754双精度二进制浮点格式) - How to convert Java double to byte[], and byte[] to double (IEEE 754 double-precision binary floating-point format) 如果Java的parseInt对于有效的IEEE-754 Binary失败,那么什么API不会呢? - If Java's parseInt fails for valid IEEE-754 Binary, what API doesn't? java 是否做了一些四舍五入以避免 IEEE-754 表示中的不精确 - Does java do some round off to avoid imprecision in IEEE-754 representation 重新审视 IEEE-754 double(64 位浮点)与 long(64 位整数) - IEEE-754 double (64-bit floating point) vs. long (64-bit integer) revisited 用于将IEEE 754 double转换为字符串的纯Java代码 - Pure Java code to convert an IEEE 754 double to string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM