June 8th, 2013, 01:01 PM
#1
How to add strings to this Code?
Last edited by harman; June 10th, 2013 at 09:48 AM .
June 8th, 2013, 02:07 PM
#2
Re: How to add strings to this Code?
Code:
import java.util.*;
public class FinalProject {
static Scanner cm = new Scanner(System.in); // accessible to all methods
public static void main (String args[]){
// 2.54cm is 1 inch
System.out.println("Enter the CM: "); // Write input
String input = cm.next();
if (input == "b" || input == "B") {
getDegree();
}
else {
//double
double centimeters = cm.nextDouble();
double inches = centimeters/2.54;
System.out.println(inches + " Inch Is " + centimeters + " centimeters");
}
}
public void getDegree() {
double celsius=0, fahrenheit=0;
System.out.println("Enter the amount of Fahrenheit to be converted: ");
fahrenheit = cm.nextDouble();
celsius = (fahrenheit-32)*5/9;
System.out.println("The entered amount of Fahrenheit is equal to " + celsius + " degrees Celsius.");
}
}
this should work all in one class. Let me know if it doesn't, I just threw in an if else statement
June 8th, 2013, 02:13 PM
#3
Re: How to add strings to this Code?
Thanks for replying but it is showing me this error >
June 8th, 2013, 02:10 PM
#4
Re: How to add strings to this Code?
I didn't put and if statement for the cm converter I just made it default if they don't type in B or b that it goes to the cm converter.
June 8th, 2013, 03:00 PM
#5
Re: How to add strings to this Code?
Try making it public static void getDegree()
June 8th, 2013, 03:07 PM
#6
Re: How to add strings to this Code?
Last edited by harman; June 10th, 2013 at 09:46 AM .
June 8th, 2013, 03:14 PM
#7
Re: How to add strings to this Code?
Attached Images
Last edited by harman; June 10th, 2013 at 09:46 AM .
June 8th, 2013, 06:12 PM
#8
Re: How to add strings to this Code?
you have to change public void getDegree() to public static void getDegree().
Code:
import java.util.*;
public class FinalProject {
static Scanner cm = new Scanner(System.in); // accessible to all methods
public static void main (String args[]){
// 2.54cm is 1 inch
System.out.println("Enter the CM: "); // Write input
String input = cm.next();
if (input == "b" || input == "B") {
getDegree();
}
else {
//double
double centimeters = cm.nextDouble();
double inches = centimeters/2.54;
System.out.println(inches + " Inch Is " + centimeters + " centimeters");
}
}
public static void getDegree() {
double celsius=0, fahrenheit=0;
System.out.println("Enter the amount of Fahrenheit to be converted: ");
fahrenheit = cm.nextDouble();
celsius = (fahrenheit-32)*5/9;
System.out.println("The entered amount of Fahrenheit is equal to " + celsius + " degrees Celsius.");
}
}
June 8th, 2013, 08:34 PM
#9
Re: How to add strings to this Code?
When comparing objects (Strings in particular) == tests for identity (ie, if the references compared refer to the same object); to test equality use the equals method. In this case you can also use equalsIgnoreCase:
Code:
if ("b".equalsIgnoreCase(input)) {
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width