I am really new to all of this stuff and I am trying to convert Celsius
to Fahrenheit
in jGRASP Java
. The code i am using is attached in the picture, also the error can be seen in the other picture.
Error message
I am really new to all of this stuff and I am trying to convert Celsius
to Fahrenheit
in jGRASP Java
. The code i am using is attached in the picture, also the error can be seen in the other picture.
Error message
The message says everything. You didn't declare F
yet, thus the compiler cannot find the symbol. Declare it before using it like
int F = 0;
EDIT: You probably meant to compare input
with the string literal "F"
. You have to declare input
as a string
, read a string
variable into it and then use the if
clause like
if (input == "F") {//...
if (input == F)
In the code which you provided, you never declare F.
Judging by the code you want to see if the users entered an "F", however you assign the input variable as such:
int input = scan.nextInt();
It would be better to do something like this:
String input = scan.nextLine();
if(input.equals("F")){
// rest of code
The problem with your code is you are telling the scanner to read an int data and you are expecting a text or a character. Using scanner.next() will return what comes before a space as a string . Then you can check it's value. Here is an example that does it.
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
String tempScale = "";
System.out.print("Enter the current outside temperature: ");
double temps = scanner.nextDouble();
System.out.println("Celsius or Farenheit (C or F): ");
String input = scanner.next();
if ("F".equalsIgnoreCase(input)) {
temps = (temps-32) * 5/9.0;
tempScale = "Celsius.";
} else if ("C".equalsIgnoreCase(input)) {
temps = (temps * 9/5.0) + 32;
tempScale = "Farenheit.";
}
System.out.println("The answer = " + temps + " degrees " + tempScale);
scanner.close();
}
And an illustration: