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

博主正在开发一个程序,用于将IEEE-754单精度和双精度浮点数转换为十进制。在尝试将二进制字符串解析为长整型或整型时,程序抛出了`java.lang.NumberFormatException`。错误出现在将二进制表示的浮点数转换为数字时,由于输入为空导致。完整的代码展示了如何进行转换,以及在选择3或4选项时遇到的输入问题。
摘要由CSDN通过智能技术生成

所以我目前正在开发一个将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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值