0
votes

So I am currently working on a program that will convert IEEE-754 single and double-precision floating point into a decimal number. The program has a java.lang.NumberFormatException thrown at it. 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. It does not let me enter an Ieee-754 number. 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
2
What is the input, what is the exception (full message), what output did you expect?assylias
It is saying that you passed an empty string. Print the ieee754 and it will probably confirm that...assylias
I'm not sure why that's the case because even if I manually enter an Ieee754 number it still comes up with the errorEdwin Lobo
Replace ieee754 = input.nextLine(); with ieee754 = "100"; and see if it works. If it does you know the problem is with input.nextLine() not doing what you expect.assylias
Yes that was the problem I just changed input.nextLine() to input.next and now it works perfect thank you.Edwin Lobo

2 Answers

1
votes

When you call

Scanner.nextInt();

followed by

Scanner.nextLine();

means the nextLine() will read the rest of the line after the number. You might not have entered anything after the number so nextLine returns empty String "" which you can see in your Exception thrown.

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. You can parse it as a Long and cast it to an (int)

You have the same problem with trying to parse a 64-bit binary as a Long. In this case you have to use a BigInteger and cast this to a long, or write your own parser.

0
votes

Your problem is here: choice = input.nextInt();

nextInt consumes an int, but not the linefeed character. 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:

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

//go on with your code

Same applies to nextDouble.

See also: Scanner issue when using nextLine after nextXXX