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

Thread: Converting primitive data types to read integers with decimal

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

    Default Converting primitive data types to read integers with decimal

    I get an error when I try to divide 500 miles by 25.5 gallons


    Exception in thread "main" java.lang.NumberFormatException: For input string: "25.5"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Mileage.main(Mileage.java:42)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 11 seconds)hen I try to divide 500miles by 25.5 gallons I get a error Message.
    import java.util.Scanner;

    import java.util.Scanner;

    public class Mileage
    {
    public static void main(String[] args)
    {
    // This Program will calculate mileage
    String milesDriven;



    String gallonsUsed;


    int milesPergallon;


    Scanner keyboard = new Scanner(System.in);

    //prompt the user for miles driven
    System.out.println("\nHow many miles have you driven already?");
    milesDriven = keyboard.nextLine();

    //prompt the user for gallons used
    System.out.println("\nHow many gallons did you use?");
    gallonsUsed = keyboard.nextLine();

    // Convert String to Interger
    milesPergallon = Integer.parseInt(milesDriven) / Integer.parseInt(gallonsUsed);
    System.out.println("\n" +milesPergallon + " " + "Miles Per Gallon"+"\n");




    }

    }



  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Converting primitive data types to read integers with decimal

    parseInt is for integers. But you are trying to parse a double. Try parseDouble.

    Something like this

    milesPergallon =(int) (Double.parseDouble(milesDriven) / Double.parseDouble(gallonsUsed));
    Last edited by camel-man; September 13th, 2014 at 08:47 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Converting primitive data types to read integers with decimal

    Wow!! Thank you so much for taking the time to help me with this issue.

    For future reference, if I come across an issue in java again is there a troubleshooting method that I should employ that could point me in the right direction on how to debug. Sincere thank you.

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Converting primitive data types to read integers with decimal

    Welcome to the forums. Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    The first step in debugging to look at the stack trace and analyse the run time exception

    Exception in thread "main" java.lang.NumberFormatException: For input string: "25.5"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Mileage.main(Mileage.java:42)

    So a 'NumberFormatException' is being thrown at Line 42 in Mileage.java. Turn on line numbers in your IDE and look at that line of code.

    milesPergallon = Integer.parseInt(milesDriven) / Integer.parseInt(gallonsUsed);

    If you don't see anything immediately wrong with this code then take a look at the API documentation for the calls you are making. Here is a link to Integer.parseInt. Have a close read. Now obviously this isn't the correct method for what you want but for educational purposes lets continue with this line of debugging. One thing that the docs say is that it will throw a NumberFormatException if the String does not contain a parsable int. So one thing we could do is catch that exception.

    try {
        milesPergallon = Integer.parseInt(milesDriven) / Integer.parseInt(gallonsUsed);
    } catch (NumberFormatException e) {
        System.err.println("Error parsing the string");
    }

    Run the program again with the same inputs. It doesn't solve the problem but at least it doesn't crash the program and gives us a way of gracefully dealing with the exception.

    Next up it's time for the debugger. All good IDE's have breakpoints. Put a breakpoint in on the problematic line and inspect the values of the variables. Now step over that line of code and see what happens. In this case you will see that Integer.parseInt(gallonsUsed) throws a NumberFormatException because it is equal to 25.5 and parseInt expects a whole number.

    This process isn't unique to your code, you will use the same basic process for pretty much every run time exception you will encounter.
    Computers are fascinating machines, but they're mostly a reflection of the people using them.
    -- Jeff Atwood

Similar Threads

  1. Conversion between primitive data types: mixed mathematical expressions
    By codingninja in forum Java Theory & Questions
    Replies: 6
    Last Post: August 7th, 2014, 07:25 AM
  2. Why Java Data types called primitive?
    By pedong in forum Java Theory & Questions
    Replies: 11
    Last Post: October 3rd, 2013, 10:20 PM
  3. Tutorial: Primitive Data Types
    By newbie in forum Java Programming Tutorials
    Replies: 3
    Last Post: July 5th, 2012, 11:56 PM
  4. Primitive types
    By sudesh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 30th, 2012, 01:44 AM
  5. [SOLVED] Converting primitive data types causing NumberFormatException
    By Melawe in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 10th, 2011, 12:30 AM