Results 1 to 9 of 9
  1. #1
    aaden is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Please help new to this need code help

    Below i have my code and 2 errors what i want to do is convert the user’s responses to double you know after the two questions i set i thought it is Double Answer = num1,num2; but error message appears please help thanks

    in case you want the question im doing it here :

    prompt the user twice to enter the distance they have driven (in miles) and how much fuel their car has used (in gallons), and it will convert the user’s responses to double.
    Add code to calculate the car’s fuel efficiency (in miles per gallon) and display this in a message dialog box.

    code below and error below that


    String Distance = JOptionPane.showInputDialog("Enter the distance you have driven");
    int num1 = Integer.parseInt(Distance);

    String Fuel = JOptionPane.showInputDialog("How much fuel has your car used");
    int num2 = Integer.parseInt(Fuel);

    Double Answer = num1,num2;

    System.out.println(Answer);
    System.out.println("Answer: " + Answer);


    System.out.println("Distance: " + Distance + "Mi");
    System.out.println("Fuel: " + Fuel + "gal");



    int mpg = num1%num2;

    JOptionPane.showMessageDialog(null, "mpg: " +mpg);

    }
    }



    error: incompatible types
    Double Answer = num1,num2;
    ^
    required: Double
    found: int

    error: variable num2 is already defined in method main(String[])
    Double Answer = num1,num2;
    ^
    2 errors

  2. #2
    aaden is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Please help new to this need code help asap


    Below i have my code and 2 errors what i want to do is convert the userís responses to double you know after the two questions i set i thought it is Double Answer = num1,num2; but error message appears please help thanks in case you want the question im doing it here :

    prompt the user twice to enter the distance they have driven (in miles) and how much fuel their car has used (in gallons), and it will convert the userís responses to double.
    Add code to calculate the carís fuel efficiency (in miles per gallon) and display this in a message dialog box.

    code below and error below that


    String Distance = JOptionPane.showInputDialog("Enter the distance you have driven");
    int num1 = Integer.parseInt(Distance);

    String Fuel = JOptionPane.showInputDialog("How much fuel has your car used");
    int num2 = Integer.parseInt(Fuel);

    Double Answer = num1,num2;

    System.out.println(Answer);
    System.out.println("Answer: " + Answer);


    System.out.println("Distance: " + Distance + "Mi");
    System.out.println("Fuel: " + Fuel + "gal");



    int mpg = num1%num2;

    JOptionPane.showMessageDialog(null, "mpg: " +mpg);

    }
    }



    error: incompatible types
    Double Answer = num1,num2;
    ^
    required: Double
    found: int

    error: variable num2 is already defined in method main(String[])
    Double Answer = num1,num2;
    ^
    2 errors

  3. #3
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: Please help new to this need code help asap

    The first error is because you are trying to set a Double variable to an Integer variable, which of course can't work. The second one can be explained ( i think ) by the fact that you are using a comma. Why are you using a comma.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Please help new to this need code help

    Regarding
    Java Code:
    Double Answer = num1,num2;
    This isn't valid Java syntax and in fact doesn't really make sense which is why the compiler is complaining. The Java compiler is very strict in what it will allow. To convert a String to double, you would use the Double equivalent to the Integer method you're using, Integer.parseInt(String intString) which is Double.parseDouble(String doubleString).

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Please help new to this need code help

    I've merged two threads here. Please go through the following links:
    Forum Rules -- particularly the second and third paragraphs
    Guide For New Members
    BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Please help new to this need code help

    You have double posted your question which is not fair to the volunteers helping here and is not allowed. I have merged the two threads together.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Please help new to this need code help

    GMTA

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Please help new to this need code help

    Whoa!

    Did we just enter some time dimensional warp?

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  2. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  3. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  4. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •