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.");
}
}
public class FinalProject {
Public static Class Convert
{
public static double CmToIn(Double Value)
{
CmToIn= Value / 2.52;
}
public static double FarToCel(Double Value)
{
FarToCel = (Value-32) * 5/9;
}
}
public static void main (String args[]){
Scanner cm = new Scanner(System.in); //Get INPUT from pc-Keyboard
System.out.println("Enter 1 for cm to inch, 2 for far to celcius"); // Write input
if (cm.nextdouble() == 1)
{
result = Convert.CmToIn(cm.NextDouble());
System.out.println("The entered amount of centimeters is equal to " + result + " inches.");
}
else
{
result = Convert.FarToCel(cm.NextDouble());,
System.out.println("The entered amount of Fahrenheit is equal to " + result + " degrees Celsius.")
}
I've never worked with java, it probably needs some adjustments
Thanks Codorna for replying but it shows errors. Please check the screen shot i attached to this.Untitled.jpg
Well, as I said I've never worked with Java... I've actually never studied java, but let's see what I can do to help
1st there shouldn't be 2 void mains, the idea here is separating the functions from the main code, so delete that first public static void main, there's also 2 public class FinalProject, remove the 1st.
2nd - I see it's case sensitive, so it's public and class, not Public and Class
3rd - nextdouble() instead of NextDouble()
4th - it seems you need to declare result, so you have to probably have to add something like Double result just above or below Scanner cm or then maybe result = new Double
if i remove any one of the FinalProject being declared twice; it shows the errors all over again Untitled.jpg
Still so many errors.
Well some one told me to do something like this below. Can you please check how it can be done for my program.
Simply prompt the user for the mode and read in a line.
import java.util.Scanner;
public class FinalProject {
public static void main (String args[]){
Double invalue;
Double result;
Scanner input = new Scanner(System.in);
System.out.println("Enter A for cm to inch, or B for far to celcius");
String option = input.nextLine();
if (option == 1){
System.out.println("How many centimeters? ") ;
invalue = intput.nextDouble();
result = invalue / 2.52;
System.out.println("The entered amount of centimeters is equal to " + result + " inches.");
}
else{
System.out.println("How many degress Farenheit? ") ;
invalue = intput.nextDouble();
result = (invalue - 32) * 5/9;
System.out.println("The entered amount of Fahrenheit is equal to " + result + " degrees Celsius.");
}
}
}
it's (
option.equals(
"B"
)) not (option == 1) sorry
We are almost there. When i hover the cursor over input.nextDouble(); it says " Type mismatch: cannot convert from double to Double." " the method nextdouble() is undefined for the type Scanner."Untitled.jpg
This is how it looks like now. When i enter "A" i am able to convert cm to inch but if i enter "B" it still comes up with cm to inch instead of fahren to celsius. Untitled.jpg
well, I know programming , languages are just tools , a problem like that would have been solved the same way in any language , you make a class/unit/set for a converter, which needs to have your converting functions, and then you will make a program that asks for numbers and use those functions to solve the problem, other than that it's just syntax , it's what i tried to do in the first answer but as I'm not sure how the syntax works so it did not work , if I had the IDE here i could have done that in just a couple minutes.
Secure Information
Content will be erased after question is completed.