Temperature Conversion in Java

Converting Fahrenheit to Celsius

Page 1 of 1

10 Replies - 4256 Views - Last Post: 21 February 2010 - 03:13 PM Rate Topic: -----

#1 draftcopy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-February 10

Temperature Conversion in Java

Posted 21 February 2010 - 02:23 PM

Hello... I am experiencing a problem with I compile this file. I receive an error that reads: <identifier> expected. Being new to Java, I'm not sure exactly what is needed. Any help would be appreciated. Thank you.

/**
* Performs a temperature conversion.
*/
public class TempConverter {

public static void fahrenheitToCelsius() {
double fTemp, cTemp;
Scanner input = new Scanner(System.in);

System.out.print("Enter a Fahrenheit temperature: ");
fTemp = input.nextDouble();
input.close();

cTemp = (double)5/(double)9*(fTemp - 32);
System.out.println("The Celsius temperature is " + cTemp);
}
input.close();

}

Is This A Good Question/Topic? 0
  • +

Replies To: Temperature Conversion in Java

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Temperature Conversion in Java

Posted 21 February 2010 - 02:26 PM

input.close()

You close the Scanner outside of the method, which isn't legal. You already closed it, so comment out this line
Was This Post Helpful? 0
  • +
  • -

#3 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Temperature Conversion in Java

Posted 21 February 2010 - 02:28 PM

You have an error because you are referring to "input" outside of its scope. You also need to import java.util.* Declare it like this:
/**
  * Performs a temperature conversion.
  */
 import java.util.*;

 public class TempConverter {

        static Scanner input = new Scanner(System.in);
        
        public static void fahrenheitToCelsius() {
		double fTemp, cTemp;
		
		System.out.print("Enter a Fahrenheit temperature: ");
		fTemp = input.nextDouble();
		input.close();
		
		cTemp = (double)5/(double)9*(fTemp - 32);
		System.out.println("The Celsius temperature is " + cTemp);
	}
	
}



I made it static to ensure it had total scope, though it's not necessarily needed.

This post has been edited by zim1985: 21 February 2010 - 02:32 PM

Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Temperature Conversion in Java

Posted 21 February 2010 - 02:29 PM

input is a Scanner object declared only inside your fahrenheitToCelsius() method.
Howver, you try to refer to it outside of this method, where it does not "exist" for the compiler.
anyway, you already closed this Scanner Object in your method, so actually you don't need this line of code.
Was This Post Helpful? 0
  • +
  • -

#5 draftcopy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-February 10

Re: Temperature Conversion in Java

Posted 21 February 2010 - 02:40 PM

Thanks for the fast replies. I changed the coding, but still receive an <identifier> expected message on line 27. ???
Was This Post Helpful? 0
  • +
  • -

#6 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Temperature Conversion in Java

Posted 21 February 2010 - 02:41 PM

View Postdraftcopy, on 21 February 2010 - 01:40 PM, said:

Thanks for the fast replies. I changed the coding, but still receive an <identifier> expected message on line 27. ???

This works:
/**
  * Performs a temperature conversion.
  */
 import java.util.*;
 public class TempConverter {

    static Scanner input = new Scanner(System.in);
	public static void fahrenheitToCelsius() 
	{
		double fTemp, cTemp;
		
		System.out.print("Enter a Fahrenheit temperature: ");
		fTemp = input.nextDouble();
		input.close();
		
		cTemp = (double)5/(double)9*(fTemp - 32);
		System.out.println("The Celsius temperature is " + cTemp);
	}
	
	public static void main(String[] args)
	{
	    TempConverter tempCon = new TempConverter();
	    tempCon.fahrenheitToCelsius();
	   }
}



Just included a driver for testing purposes...

This post has been edited by zim1985: 21 February 2010 - 02:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Temperature Conversion in Java

Posted 21 February 2010 - 02:42 PM

View Postdraftcopy, on 21 February 2010 - 05:40 PM, said:

Thanks for the fast replies. I changed the coding, but still receive an <identifier> expected message on line 27. ???

Post your code.

This post has been edited by anonymouscodder: 21 February 2010 - 02:45 PM

Was This Post Helpful? 0
  • +
  • -

#8 draftcopy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-February 10

Re: Temperature Conversion in Java

Posted 21 February 2010 - 02:58 PM

Here's what I have now, and I get: java.lang.NoSuchMethodError: main
Exception in thread "main"




public class TempConverter {
 	
 	static Scanner input = new Scanner (System.in);
 	public static void fahrenheitToCelsius()
 	{
		double fTemp, cTemp;
		
		System.out.print("Enter a Fahrenheit temperature: ");
		fTemp = input.nextDouble();
		input.close();
		
		cTemp = (5.0/9.0)*(fTemp - 32);
		System.out.println("The Celsius temperature is " + cTemp);
	}
}


Was This Post Helpful? 0
  • +
  • -

#9 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Temperature Conversion in Java

Posted 21 February 2010 - 03:04 PM

View Postdraftcopy, on 21 February 2010 - 01:58 PM, said:

Here's what I have now, and I get: java.lang.NoSuchMethodError: main
Exception in thread "main"




public class TempConverter {
 	
 	static Scanner input = new Scanner (System.in);
 	public static void fahrenheitToCelsius()
 	{
		double fTemp, cTemp;
		
		System.out.print("Enter a Fahrenheit temperature: ");
		fTemp = input.nextDouble();
		input.close();
		
		cTemp = (5.0/9.0)*(fTemp - 32);
		System.out.println("The Celsius temperature is " + cTemp);
	}
}


There's no main thread here. If that's where your issues are, post it please.
Was This Post Helpful? 1
  • +
  • -

#10 draftcopy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 21-February 10

Re: Temperature Conversion in Java

Posted 21 February 2010 - 03:13 PM

Hey, thank you so much! It worked... after discovering I typed a line wrong. :|
Was This Post Helpful? 0
  • +
  • -

#11 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Temperature Conversion in Java

Posted 21 February 2010 - 03:13 PM

View Postdraftcopy, on 21 February 2010 - 05:58 PM, said:

Here's what I have now, and I get: java.lang.NoSuchMethodError: main
Exception in thread "main"


A Java application always start by the main method. This should work:
import java.util.Scanner;

public class TempConverter {
        
        static Scanner input = new Scanner (System.in);
        public static void fahrenheitToCelsius()
        {
                double fTemp, cTemp;
                
                System.out.print("Enter a Fahrenheit temperature: ");
                fTemp = input.nextDouble();
                input.close();
                
                cTemp = (5.0/9.0)*(fTemp - 32);
                System.out.println("The Celsius temperature is " + cTemp);
        }

	public static void main(String[] args) {
		fahrenheitToCelsius();
	}
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1