Results 1 to 12 of 12
Thread: How to add strings to this Code?
- 06-08-2013, 07:34 PM #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.");
}
}
- 06-08-2013, 07:49 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,170
- Rep Power
- 12
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 }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 06-17-2013, 08:25 PM #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.
- 06-17-2013, 09:52 PM #4
Senior Member
- Join Date
- Apr 2013
- Location
- Sweden
- Posts
- 272
- Rep Power
- 5
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.
- 06-17-2013, 09:56 PM #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 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. } }
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.
- 06-17-2013, 10:02 PM #6
Senior Member
- Join Date
- Apr 2013
- Location
- Sweden
- Posts
- 272
- Rep Power
- 5
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
- 06-18-2013, 07:41 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
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.
JossBuild a wall around Donald Trump; I'll pay for it.
- 06-18-2013, 02:25 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,170
- Rep Power
- 12
Re: How to add strings to this Code?
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
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 06-18-2013, 02:48 PM #9
Senior Member
- Join Date
- Apr 2013
- Location
- Sweden
- Posts
- 272
- Rep Power
- 5
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.
- 06-18-2013, 02:55 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 06-18-2013, 04:58 PM #11
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,170
- Rep Power
- 12
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 06-18-2013, 05:07 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
Re: How to add strings to this Code?
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,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
Strings
By Krazzyjman in forum New To JavaReplies: 3Last Post: 09-07-2012, 08:14 PM -
Strings
By leepikamukharji in forum New To JavaReplies: 12Last Post: 04-18-2011, 01:43 PM -
Need help with Strings
By </3java in forum New To JavaReplies: 24Last Post: 02-17-2011, 05:00 PM -
It is possible in Strings..?
By mlibot in forum New To JavaReplies: 1Last Post: 03-12-2010, 05:30 AM -
Help with Code! Display Array of Strings and Integers - Sorted
By luvjoynt in forum New To JavaReplies: 7Last Post: 04-28-2008, 04:28 AM
Bookmarks