Results 1 to 9 of 9
- 10-21-2012, 09:20 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
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
- 10-21-2012, 09:23 PM #2
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
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
- 10-21-2012, 09:53 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 49
- Rep Power
- 0
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.
-
Re: Please help new to this need code help
Regarding
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).Java Code:Double Answer = num1,num2;
- 10-22-2012, 03:51 AM #5
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
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.
-
Re: Please help new to this need code help
Lord, we attempted to merge at the same time!
- 10-22-2012, 04:18 AM #8
Re: Please help new to this need code help
GMTA
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-22-2012, 04:50 AM #9
Similar Threads
-
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01-31-2012, 08:36 PM -
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks