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 8 of 8

Thread: Parsing Scientific Notation from String

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,604
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Parsing Scientific Notation from String

    I am trying to figure out how to parse a number into a double which represented as a String in scientific notation.

    For example, I have the number: 8.86162677393184E-02.

    When I do a Double.parseDouble(String) call on it, I get this error:
    java.lang.NumberFormatException: For input string: "E-2"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
    at java.lang.Double.parseDouble(Double.java:510)
    at RunDB.displayResults(RunDB.java:208)
    at RunDB.executeChangeQuery(RunDB.java:151)
    at RunDB.<init>(RunDB.java:55)
    at CPLFProgram.main(CPLFProgram.java:17)
    Can anyone help me out?
    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/


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Parsing Scientific Notation from String

    See the DecimalFormat class. You can create the class with a specified format, then parse into a number. Give the code below a shot
    String file = "8.86162677393184E-02";
    try{
    DecimalFormat format = new DecimalFormat("#.##");		
    System.out.println(format.parse(file).floatValue());
    }catch(Exception e){
        e.printStackTrace();
    }

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,604
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: Parsing Scientific Notation from String

    Nope, failed.

    This is the error now:
    java.text.ParseException: Unparseable number: "E-2"
    at java.text.NumberFormat.parse(NumberFormat.java:333 )
    at RunDB.executeChangeQuery(RunDB.java:209)
    at RunDB.<init>(RunDB.java:57)
    at CPLFProgram.main(CPLFProgram.java:17)
    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/

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Parsing Scientific Notation from String

    Hmmm, worked for me. Is the exception being thrown with the above number, or something else (post the something else if that is the case)? You may have to fiddle with the DecimalFormat object a bit, but it should be doable.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,604
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: Parsing Scientific Notation from String

    Yep, the number above and about a thousand other numbers.

    What strikes me as odd is I think the 0 between the - and the 2 is just being thrown away (possibly the cause of the exception).
    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/

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Parsing Scientific Notation from String

    In java, a blank space between the exponent and the base or power are not allowed. For example, these are not allowed: (assume under-scores represent spaces)
    1.23_e5
    1.23e_5
    1.23e_-5
    1.23e-_5
    1.23e_-_5

    check your data and make sure that there are indeed no blank spaces (or any other characters for that matter)

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,604
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: Parsing Scientific Notation from String

    Ok, so what the hell happened to the 0 when I told it to parse the number?
    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/

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,604
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: Parsing Scientific Notation from String

    Ok, this has translated into a database issue that I have no idea what to do.

    I am retrieving these numbers from a database. As it turns out, when it reads in the values from the database, it is not reading in the whole number, just the E-2. Is there any way to fix this?

    I am reading in the number with the ResultSet.getString(int) method and attempting to parse it. I have tried using the ResultSet.getBigDecimal(int) method, but that throws the following error:
    java.lang.NumberFormatException
    	at java.math.BigDecimal.<init>(BigDecimal.java:505)
    	at java.math.BigDecimal.<init>(BigDecimal.java:728)
    	at sun.jdbc.odbc.JdbcOdbcResultSet.getBigDecimal(JdbcOdbcResultSet.java:1957)
    	at RunDB.executeChangeQuery(RunDB.java:206)
    	at RunDB.<init>(RunDB.java:58)
    	at CPLFProgram.main(CPLFProgram.java:17)


    Since the problem has actually merged with my other current issues, I'm going to cross-post this with the topic I created for the database a few days ago: Connecting to Access 2007 Database
    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/

Similar Threads

  1. Parsing CDATA
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 9th, 2010, 12:33 AM
  2. java xml-rpc response parsing to xml
    By kievari in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: November 19th, 2009, 01:36 PM
  3. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 09:23 AM
  4. Printing xml to the console from .wmdb without printing junks
    By John in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: April 24th, 2009, 03:44 AM
  5. [SOLVED] Parsing ID3 tags from mp3
    By John in forum Java Theory & Questions
    Replies: 14
    Last Post: April 16th, 2009, 01:36 PM