Results 1 to 9 of 9
Thread: simple code not compiling
- 09-30-2010, 04:29 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
simple code not compiling
I am trying to run a program that will ask for inputs of p or P for pounds and k or K for kilograms, and then a number for the weight, and then convert that to the other measure.
It should run a program that should handle both upper-case and lower-case input (p or P, k or K)
and should print an error if the user enters anything except
those options.
To convert from pounds to kilograms, you multiply the weight in pounds by 0.454. To convert
from kilograms to pounds, you multiply the weight in kilograms by 2.205.
Additionally, you should format the output to round the resulting weight to the nearest three
decimal places.
import java.util.*;
import java.text.*;
public class Proj2
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#0.000");
System.out.print("Enter (p)ounds or (k)ilograms: ");
char m = s.nextLine().charAt(0);
System.out.print("Enter weight in pounds: ");
double num = Double.parseDouble(s.nextLine());
System.out.println("");
double kilograms = num * 0.454;
double pounds = num * 2.205;
char P;
char p;
char K;
char k;
if (m = P || m = p){
String lbs = Character.toString(m);
System.out.println(num + m + "is equivalent to" + kilograms + m);
}
else if (m = K || m = k) {
String kg = Charcter.toString(m);
System.out.println(num + m + "is equivalent to" + pounds + m);
}
else {
System.out.print("Invalid option.");
}
}
}Last edited by cliffh; 09-30-2010 at 04:51 AM.
- 09-30-2010, 04:46 AM #2
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
I've tried to change a few lines and I think I might have made it worse. Can anyone help with getting it to compile?
- 09-30-2010, 05:01 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 21
- Rep Power
- 0
a) check if else , you dont check if they are equal, all you do is to give new value, check the difference between one = and two =
b)consider why your chars P,p,K, and k are null ;)
- 09-30-2010, 05:17 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
I'm not exactly sure what you are saying... I used == instead of = but didn't do anything, I also set char P,p,K,k = m. I'm just confused because the examples we did in class were nothing like this and I've had no experience with this before.
- 09-30-2010, 05:24 AM #5
The problem is you never set the values of P, p, K and k. If you want to do a char comparison, wrap the letter in single quotes ( ' ). Example: 'K'
So it's gonna look something like this:
if (m == 'k') {
More on primitives (including char): Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
PS: If something isn't compiling (there's a difference between a runtime logic error, a runtime execution error, and a compiler error), then please provide the full error message with line numbers & any other information included.
- 09-30-2010, 05:30 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
yep that worked. thanks a bunch!
- 09-30-2010, 05:36 AM #7
- 09-30-2010, 05:37 AM #8
He just didn't repost his code after changing them. Regardless, he says it is all working now.
- 09-30-2010, 05:47 AM #9
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
import java.util.*;
import java.text.*;
public class Proj2
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#0.000");
System.out.print("Enter (p)ounds or (k)ilograms: ");
char m = s.nextLine().charAt(0);
System.out.print("Enter weight in pounds: ");
double num = Double.parseDouble(s.nextLine());
System.out.println("");
double kilograms = num * 0.454;
double pounds = num * 2.205;
if (m == 'P' || m == 'p'){
System.out.println(num + " lbs is equivalent to " + df.format(kilograms) + " kg");
}
else if (m == 'K' || m == 'k') {
System.out.println(num + " kilograms is equivalent to " + df.format(pounds) + " lbs");
}
else {
System.out.print("Invalid option.");
}
}
}
Similar Threads
-
my code compiling but not running
By girishkumar in forum New To JavaReplies: 16Last Post: 03-16-2010, 04:45 PM -
compiling error should be a simple fix.
By bottlecap in forum New To JavaReplies: 2Last Post: 01-23-2010, 06:07 AM -
Help me out in compiling the source code
By aks.nitw in forum Advanced JavaReplies: 3Last Post: 10-17-2008, 08:33 AM -
Trouble compiling code
By waelhelbawi in forum New To JavaReplies: 1Last Post: 05-12-2008, 04:25 AM -
Compiling and running code in runtime
By tim in forum New To JavaReplies: 4Last Post: 01-27-2008, 06:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks