Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java.lang.NumberFormatException thrown

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry Java.lang.NumberFormatException thrown

    I am trying to write a program for converting positive binary inputs into hex.
    Why am i getting this errors while compiling my binary to hex converter..

    Exception in thread "main" java.lang.NumberFormatException: For input string: "148.0"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:441)
    at BinToHex.convertbintohex(BinToHex.java:24)
    at Test.main(Test.java:4)


    Here is my BinToHex class

    import java.io.*;
     
     
        public class BinToHex {
    	    double tempDec,fractionpart;
    	    long longofintpart,templongDec;
    	    String inpu ="1001.01";
    	    String hexOutput,intpart,tempDecString,hex = null;
     
    	    static int i = 1;
     
    	    public void convertbintohex() {
     
    			if (inpu.contains(".")) {
    				int placesAfterPoint = inpu.length() - inpu.indexOf(".") - 1;//every thing
    				long numerator = Long.parseLong(inpu.replace(".", ""), 2);//goes 
    				double decimalOfInput = ((double) numerator) / (1L << placesAfterPoint);//alright  till here 
     
     
    				while (true) {
    					tempDec = decimalOfInput * 16;
    					if ((int)tempDec == tempDec) {
    						tempDecString = String.valueOf(tempDec);
    						templongDec = Long.parseLong(tempDecString, 10);
    						hexOutput = Long.toHexString(templongDec);
     
    						break;
    					} else {
    						intpart = String.valueOf((long)tempDec);
    						longofintpart = Long.valueOf(intpart).longValue();
    						if(i==1){
    							hex=Long.toHexString(longofintpart);
    							hexOutput = hex + ".";
    							i=i+1;
    						}else{
    							hexOutput = hexOutput + hex;
    						}
    						fractionpart = tempDec-(int)tempDec;
    						decimalOfInput = fractionpart;
    					}
    				}
    			} else {
    					// this part is ok
    				tempDecString = String.valueOf(Integer.parseInt(inpu, 2));
    				templongDec = Long.parseLong(tempDecString, 10);
    				hexOutput = Long.toHexString(templongDec);
    			}
    			System.out.println(hexOutput);
    	    }	
       }

    and my Test class..

     public class Test{
    	public static void main(String args[]){
    		BinToHex i = new BinToHex();
    		i.convertbintohex();	
    	}
    }

    my first question
    really need help


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java.lang.NumberFormatException thrown

    Long values can't contain a decimal point.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Tiash (September 29th, 2014)

  4. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Java.lang.NumberFormatException thrown

    You need to get rid of the decimal places after the number. You cannot convert a number, represented as a String, which has a decimal place on it to a Long (at least not the way you are doing it).
    One solution would be to get the double value, and then type cast to a long. For reference, this piece of code:
    (int)tempDec
    Is a type cast from double to int.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Tiash (September 29th, 2014)

Similar Threads

  1. Replies: 2
    Last Post: July 2nd, 2013, 04:59 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. (java.lang.NumberFormatException: null) error
    By vishnuib2000 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: April 2nd, 2012, 08:09 AM
  4. java.lang.numberformatexception error
    By natalie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 19th, 2010, 04:16 AM
  5. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM

Tags for this Thread