How to Add strings to this code?

Programming
Tutor: None Selected Time limit: 3 Days

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



}
}

Nov 30th, -0001
It would be something like this :

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
Jun 8th, 2013

Thanks Codorna for replying but it shows errors. Please check the screen shot i attached to this.Untitled.jpg

Jun 8th, 2013

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

Jun 8th, 2013

I solved the other errors and now i am only left with this one >  Untitled.jpg

Jun 8th, 2013

probably due to FinalProject being declared twice

Jun 8th, 2013

if i remove any one of the FinalProject being declared twice; it shows the errors all over again Untitled.jpg

Jun 8th, 2013
Jun 8th, 2013

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.

Java Code:

1

2

3

4

5

6

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.

Jun 8th, 2013

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.");
  }
   
  }
}
 

Jun 8th, 2013

it's (option.equals("B")) not (option == 1) sorry

Jun 8th, 2013

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


Jun 8th, 2013

it's a lowercase / uppercase Issue , looks like nextdouble should be nextDouble

Jun 8th, 2013

OMG!! Thank you very very much. You did it.

Jun 8th, 2013

you're welcome, good luck learning.

Jun 8th, 2013

Oops. No matter what letter i enter it still converts Fahrenheit to Celsius.

Jun 8th, 2013

if you changed option == 1 to (option.equals("B")) then it should check if it's an uppercase B , it should be option.equals("A") i guess


Jun 8th, 2013

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

Jun 8th, 2013

change option == "B" to option.equals("A")

Jun 8th, 2013

Now showing error after changing option == "B" to option.equals("A")Untitled.png

Jun 8th, 2013

missing a )

Jun 8th, 2013

sorry which missing a )

Jun 8th, 2013

(option.equals("A") should be

(option.equals("A"))

Jun 8th, 2013

Yea, i corrected it but everything after else statement is on error Untitled.jpg

Jun 8th, 2013

missing a { in front of (option.equals("A"))

Jun 8th, 2013

I don't know how to thank you. This was actually my final assignment before end of the school and my teacher didn't taught us really well so now my entire class is looking for help online. Thank you very much.

Jun 8th, 2013

you're welcome, good luck learning.


My recommendation is learning logic and pseudo code , my help was lousy because I've never studied / tried Java (I actually work with eletronics / delphi), but once you learn logic it's very easy to understand the languages.

Jun 8th, 2013

I wonder how you know Java without even learning it. Did you learn any programming language that made it easy for you to work with Java?

Jun 8th, 2013

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.

Jun 8th, 2013

Are you studying on the go? Check out our FREE app and post questions on the fly!
Download on the
App Store
...
Nov 30th, -0001
...
Nov 30th, -0001
Dec 3rd, 2016
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer