Results 1 to 12 of 12
  1. #1
    singh is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default How to add strings to this Code?

    Hi, I already have the code below but i have to do the following with it. I am making a Unit convertor program where I can convert CM to Inch and Fahrenheit to Celsius in same program.
    I want to use strings. For example:- User will have different options for the unit converter they want to use.

    Such as:

    Option A: cm to inch
    Option B: Fahrenheit to Celsius

    If user enters letter “B” than program will convert Fahrenheit to Celsius. User will be asked to enter a value in Fahrenheit and once the value is entered it will convert it to Celsius.

    Here is the code:-

    CM to Inch

    import java.util.Scanner;

    public class FinalProject {
    public static void main (String args[]){
    // 2.54cm is 1 inch
    Scanner cm = new Scanner(System.in); //Get INPUT from pc-Keyboard
    System.out.println("Enter the CM:"); // Write input
    //double
    double centimeters = cm.nextDouble();
    double inches = centimeters/2.54;
    System.out.println(inches + " Inch Is " + centimeters + " centimeters");


    }
    }

    Fahrenheit to Celsius



    double celsius=0, fahrenheit=0;
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter the amount of Fahrenheit to be converted: ");
    fahrenheit = scan.nextDouble();

    celsius = (fahrenheit-32)*5/9;

    System.out.println("The entered amount of Fahrenheit is equal to " + celsius + " degrees Celsius.");



    }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: How to add strings to this Code?

    Simply prompt the user for the mode and read in a line.
    Java Code:
    String option = scan.nextLine();
    if (option.equals("B")) {
      //  do something
    } else {
       // do something else
    }
    Note: You can use the same scanner instance for both conversion routines. Also check out using Strings in the tutorial in my signature.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: How to add strings to this Code?

    Like jim829 said, just use the if/else set, and make sure you use the .equals() method, == does not work for strings. Also, if you want to use strings for your numbers as well, (like String CM = "5") you can use the parseDouble(CM), which will parse a number value from a string, if it can, which also works for other variable types, like int and so forth. One last thing, use the code tags (#) when posting code, it makes makes it much easier to follow.

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: How to add strings to this Code?

    Well, == works for strings, it is overloaded; however you should use the equals method and use conditions.
    For practice, you can also try to make the program in dialog boxes using JOptionPane tutorial: How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    apart from the console version.

  5. #5
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: How to add strings to this Code?

    ill complete it for you.

    Java Code:
    import java.util.Scanner;
    
     public class FinalProject {
     public static void main (String args[]){
     // 2.54cm is 1 inch
     Scanner cm = new Scanner(System.in); //Get INPUT from pc-Keyboard
     String answer = cm.nextDouble();
     if(answer.equalsIgnoreCase("a"){
    
     System.out.println("Enter the CM:"); // Write input
     //double
     double centimeters = cm.nextDouble();
     double inches = centimeters/2.54;
     System.out.println(inches + " Inch Is " + centimeters + " centimeters");
    
    
     }//end of the if-statement for the string.
     }
     }
    Fahrenheit to Celsius



    Java Code:
    double celsius=0, fahrenheit=0;
     Scanner scan = new Scanner(System.in);
     String answer = scan.nextDouble();
     if(answer.equalsIgnoreCase("B"){
    
     System.out.println("Enter the amount of Fahrenheit to be converted: ");
     fahrenheit = scan.nextDouble();
    
     celsius = (fahrenheit-32)*5/9;
    
     System.out.println("The entered amount of Fahrenheit is equal to " + celsius + " degrees Celsius.");
    
    
     }//end of the if-statement for the string.
     }
     }

    i think this should work.
    Last edited by JosAH; 06-18-2013 at 07:38 AM.

  6. #6
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: How to add strings to this Code?

    @MR Bruto, you do realize that you post uncompilable code with errors.
    And besides that, you shouldn't be giving solutions in the first place

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to add strings to this Code?

    @MR Bruto, don't edit away the previous contents of your (incorrect) reply; it ruins the flow of discussion and lowers the value of this thread; others might plunge in here through google or similar, only to find fragments of what was here at a particular point in time.

    Joss
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: How to add strings to this Code?

    Quote Originally Posted by superhaNds View Post
    Well, == works for strings, it is overloaded; however you should use the equals method and use conditions.
    Actually its just the opposite. Because == is not overloaded, it does not work for Strings or any other object unless you are checking to see if the object is either null or the same object as another. Try this:
    Java Code:
    String a = new String("Hello");
    String b = new String("Hello");
    System.out.println(a == b); // will print false
    System.out.println(a.equals(b)); // will print true
    Similarly you can't use any of the relational operators (e.g. <=, >=) with Strings. You have to use the compareTo method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: How to add strings to this Code?

    = , == , + are overloaded is the Java API

    String s1 = "a";
    String s2 = "a";
    String s3 = null;
    if (s1 == s2)
    s3 = s1 + s2;
    System.out.println(s1 + " " + s2 + " " + s3);

    If you use the String constructor like you did, it doesn't work.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to add strings to this Code?

    The == operator is overloaded for the primitive types and any reference type, not just the String type. To complicate matters even further, the == operator (seems to) work for literal Strings and intern()ed Strings too. (the same holds for some small Integers).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: How to add strings to this Code?

    If == is overloaded then what is it overloaded to do? You can't use it consistently to check for equality of strings or any other object. To me, if it were really overloaded, you could use it in place of equals and the JRE would recognize that and perhaps silently invoke equals for the comparison. I would equate this to overloading the math operators in support of BigInteger. At least that is my understanding of overloading an operator.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to add strings to this Code?

    Quote Originally Posted by jim829 View Post
    If == is overloaded then what is it overloaded to do? You can't use it consistently to check for equality of strings or any other object. To me, if it were really overloaded, you could use it in place of equals and the JRE would recognize that and perhaps silently invoke equals for the comparison. I would equate this to overloading the math operators in support of BigInteger. At least that is my understanding of overloading an operator.
    The operators are overloaded but not in a way beginners expect; comparing two, say bytes, generates different code than comparing two objects. Also * or >> etc. are overloaded; e.g. shfting a long takes different code from shifting an int ...

    This is only one reason I'm very much against user defined operator overloading in a language ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Strings
    By Krazzyjman in forum New To Java
    Replies: 3
    Last Post: 09-07-2012, 08:14 PM
  2. Strings
    By leepikamukharji in forum New To Java
    Replies: 12
    Last Post: 04-18-2011, 01:43 PM
  3. Need help with Strings
    By </3java in forum New To Java
    Replies: 24
    Last Post: 02-17-2011, 05:00 PM
  4. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 05:30 AM
  5. Replies: 7
    Last Post: 04-28-2008, 04:28 AM

Posting Permissions

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