1. Member
Join Date
Jun 2013
Posts
1
Rep Power
0

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,748
Rep Power
10

## 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

3. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

## 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. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
4

## 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. ## 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

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

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. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
4

## 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. ## 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

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,748
Rep Power
10

## Re: How to add strings to this Code?

Originally Posted by superhaNds
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

9. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
4

## 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. ## 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

11. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,748
Rep Power
10

## 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

12. ## Re: How to add strings to this Code?

Originally Posted by jim829
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

#### Posting Permissions

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