I keep getting errors saying that the scanner statement is not a statement.
I am also getting more errors such as, expecting ";" on the scanner line and that my celsius statement which does the math to convert Celsius into Fahrenheit. I need help correcting these errors

import java.util.*;
import java.txt.*;

public class TempConvert{
	public static void main(String arg[]){
		double celsius, fahrenheit, num=0, num1;
		Scanner new = new Scanner(System.in);
		TempConvert pn = new Tempconvert();
		System.out.printIn("Will you be using converting to Fahrenheit or Celsius? ");
		num = scan.nextInt();
		System.out.printIn("Enter your Temperature");
		num1 =scan.nextINt();
			if(num = fahrenheit){
				pn.CtoF();
			}// end first if statement
			if(num1 = celsius){
				pn.FtoC();
			}// ends second if statement
	
			


	}// end main()


	public void FtoC(){
		double celsius, fahrenheit;
		celsius = 5/9(fahrenheit - 32);
		System.out.printIn("Your tempeture in Celsius is " + celsius);
		}// end FtoC

	public void CtoF(){
		double celsius, fahrenheit;
		fahrenheit = celsius * (9/5) + 32;
		System.out.printIn("Your tempeture in Fahrenheit is " + fahrenheit);
		}// end CtoF

Recommended Answers

All 12 Replies

You are trying to use a java keyword as a variable name. Scanner new = new Scanner() does not make any sense. new is a java keyword. Change the variable name to something else.
Here is a list of keywords which cannot be used as variable names
http://download.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

Also,

if(num = fahrenheit){
pn.CtoF();
}// end first if statement
if(num1 = celsius){
pn.FtoC();
}// ends second if statement

This is not how you use if statements. You need to check whether a condition is true or false. What you are doing here is equating num,num1 to equal the other variables. You are not testing if they are equal. To test equality, you need to use special equality or conditional operators. So, to test equality between num and fahrenheit, you would use
if(num==fahrenheit)
Check this for a list of operators in java: http://download.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

Wherever/however you are getting taught java, they/you are not doing it correctly

ok i found my mistakes and corrected them.but now im getting a error about my "num = scan.nextInt();" here is the updated program

import java.util.*;

public class TempConvert{
	public static void main(String arg[]){
		double celsius, fahrenheit, num=0, num1;
		Scanner scan = new Scanner(System.in);
		TempConvert pn = new TempConvert();
		System.out.println("Will you be using converting to Fahrenheit or Celsius? ");
		num = scan.nextInt();
		
			if(num == fahrenheit){
				pn.CtoF();
			}// end first if statement
			if(num1 == celsius){
				pn.FtoC();
			}// ends second if statement
	
			


	}// end main()


	public void FtoC(){
		double celsius, fahrenheit, num=0;
		System.out.println("Enter your Temperature");
		num = scan.nextInt();
		celsius = 5/9*(fahrenheit - 32);
		System.out.println("Your tempeture in Celsius is " + celsius);
		}// end FtoC

	public void CtoF(){
		double celsius, fahrenheit, num=0;
		System.out.println("Enter your Temperature");
		num = scan.nextInt();
		fahrenheit = celsius * (9/5) + 32;
		System.out.println("Your tempeture in Fahrenheit is " + fahrenheit);
		}// end CtoF
}

num is declared as a double. You are trying to read in nextInt(). Change to scan.nextDouble().

Hello - I took your updated program and checked it and I get several errors before compiling. First of all in the main method, fahrenheit, num1 and celsius aren't initialised, therefore when you call the first if statement you are asking:

if num( num is equal to one) is equal to fahrenheit (fahrenheit has no value and you aren't asking for a value either so your program will return an error)

Second when you declare scan as an instance of the Scanner class to accept input you only do it in the main method which means that all methods there after can't use it. To rectify this you could place the statement before the main method and declare it as static, thus allowing all methods to use the Scanner class.

Finally, Akill10 is right. You can't declare a variable as double and then use the input for an integer to read it, it will give you a runtime error like this:

Exception in thread "main" java.util.InputMismatchException

Hope this helps :P

Thanks this did help i have all my syntax right but now when i run the program I can do all the input but i do not get any output. Maybe there is something wrong with my method calls... Here is the updated version.....

import java.util.*;

public class TempConvert{
	public static void main(String arg[]){
		double celsius=0, fahrenheit=0, num=0;
		Scanner scan = new Scanner(System.in);
		TempConvert pn = new TempConvert();
		System.out.println("Will you be using converting to Fahrenheit or Celsius? Press 1 for Celsius and 2 for Feahrenheit. ");
		num = scan.nextDouble();
		System.out.println("Enter your Temperature");
		num = scan.nextInt();
		
			if(num == 1){

				pn.CtoF();
			}// end first if statement
			if(num == 2){

				pn.FtoC();
			}// ends second if statement
		
	
			


	}// end main()


	public void FtoC(){
		double celsius=0, fahrenheit=0, num=0;
		celsius = 5/9*(fahrenheit - 32);
		System.out.println("Your tempeture in Celsius is " + celsius);
		}// end FtoC

	public void CtoF(){
		double celsius=0, fahrenheit=0, num=0;
		fahrenheit = celsius * (9/5) + 32;
		System.out.println("Your tempeture in Fahrenheit is " + fahrenheit);
		}// end CtoF
}

Can I ask you where/how you are learning programming/Java? The mistakes you are making are elementary. You are using your methods incorrectly, you are using your variables incorrectly. Not that I mind helping you, but please tell me how you are learning java so I can let people know it is not a good way to go.

num = scan.nextDouble();
System.out.println("Enter your Temperature");
num = scan.nextInt();

Can you not see how this is wrong? Apart from the fact we told you that you declared num as a double and therefore cannot read an int into it, you are using the exact same variable consecutively. What do you think will happen here?

#
public void FtoC(){
double celsius=0, fahrenheit=0, num=0;
celsius = 5/9*(fahrenheit - 32);
System.out.println("Your tempeture in Celsius is " + celsius);
}// end FtoC

How on earth did you come up with this?Can you not see that celsius will be 0? even a beginner can realise this will do nothing. You are redeclaring all your variables aswell. Why even have those same variables in your main method? All you would need are variables for the input you take. Then keep the celsius/fah variables local to your methods as you have there.

If you want to use variables that you have declared and set outside of your target method, then you need to pass those variables into your target method as parameters.

public void FtoC(double fah){//takes a double as a paramater(i.e. the temperature entered in fahrenheit)
double celsius = 0;
celsius = 5/9*(fah- 32);
System.out.println("Your tempeture in Celsius is " + celsius);
}

Then to call this method:

if(num == 2){
pn.FtoC(fahrenheit);
}// end first if statement

So, There are a list of basic things wrong with your code(that a programmer with 1 week experience could probably pick up). Again I ask you where/who is teaching you?

Akill10 do you teach java? Im curious as you speak/type like a teacher :P

the renard from class...change 5 and 9 to 5.0 and 9.0..it should work

Akill10 do you teach java? Im curious as you speak/type like a teacher :P

No, I'm still a student, I wasn't aware a teacher typed a certain way! I'm not even sure if I should take that as a compliment, but hey, I will take what I can get. :)

the renard from class...change 5 and 9 to 5.0 and 9.0..it should work

Could you please elaborate as to what would "work" ?

No, I'm still a student, I wasn't aware a teacher typed a certain way! I'm not even sure if I should take that as a compliment, but hey, I will take what I can get. :)

I didn't mean it in a derogatory way; just seemed like how a teacher spoke. I just expected a detention to come along with it lol :P

One piece of confusion that needs to be cleared up here is variable scope. Briefly:

Variables exist in the context in which they are declared, and any context contained within that context. Context is defined by curly braces. (more precisely, they exist from the moment they are declared until the right curly brace closing the block in which they are declared, but we're all sensible people and declare our variables up front, so it comes to the same thing, right?)

Side note: Variables declared in the top level of a class file are special in that they are visible throughout the class (as you'd expect from the above) and in that they are potentially visible to other classes. You don't need to worry about this here, but this is what you're seeing when you see modifiers like "public" and "private" - these tell the compiler whether other classes can read and change these variables directly. End of tangent.

Since a variable only exists within its local context, a variable declared in one method cannot be referenced from another context. It can be passed to the other method as a parameter, but it cannot be referenced directly.

So, when you get your input in one method and work on it in another (this is the correct design, by the way) you need to get the data from one method to another by one of two methods.

a) Declare the data variables as class fields (top-level, declared before your main method).
This is potentially awkward, since you're running the program out of main, which is a static method. We'll go into why this is another time, if you want, but for the moment I assert and you should believe that this means that you have to declare your fields as "static" to use them from main. This is not a good solution in terms of design, but it'll make things work for now - you'll be able to get at "celsius", "fahrenheit", and "num" from anywhere in the program, provided you haven't "shadowed" them by declaring new variables with the same name within a method .

A better solution is:
b) pass the data to your work methods as a parameter. cToF() needs to know one thing: your initial celsius temperature. So it should take a double as a parameter, and do its math on that. The method signature of cToF would be:

public void cToF(double celsius)
{
}

A better piece of design would be:

public double cToF(double celsius)
{
  // calculate fahrenheit value
  return fahrenheit
}

This is better because it does one thing: it calculates the value you're looking for and returns it. Your method does two things: it calculates the value and then prints it, which means you have no way of just getting the value (perhaps to put it in a dialog box or store it as a data point in an array).

You can then write a convenience method if you need it:

public void printCToF( double celsius)
{
  System.out.println("C: "+celsius+" F: "cToF(celsius));
}

That'll call your work method to do the conversion, but now you've isolated your calculation so you can use it again elsewhere if you need it later.

(And no, I'm not a teacher either! :) )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.