Results 1 to 9 of 9
  1. #1
    cliffh is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default 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 05:51 AM.

  2. #2
    cliffh is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    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?

  3. #3
    jlmp is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    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 ;)

  4. #4
    cliffh is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    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.

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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.

  6. #6
    cliffh is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    yep that worked. thanks a bunch!

  7. #7
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Quote Originally Posted by cliffh View Post
    I used == instead of = but didn't do anything, I also set char P,p,K,k = m.
    Where did you made those changes, I don't see it in your code.
    And for '==' and '=' they are different.
    Your if and else conditions are wrong.

    -Regrads

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    He just didn't repost his code after changing them. Regardless, he says it is all working now.

  9. #9
    cliffh is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    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

  1. my code compiling but not running
    By girishkumar in forum New To Java
    Replies: 16
    Last Post: 03-16-2010, 05:45 PM
  2. compiling error should be a simple fix.
    By bottlecap in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 07:07 AM
  3. Help me out in compiling the source code
    By aks.nitw in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 09:33 AM
  4. Trouble compiling code
    By waelhelbawi in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 05:25 AM
  5. Compiling and running code in runtime
    By tim in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 07:58 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
  •