# Temperature Conversion

Page 1 of 1

## 9 Replies - 1196 Views - Last Post: 06 April 2010 - 09:23 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=166564&amp;s=a53c082acb9e41d66dc4fe4f84b55903&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mothan

• New D.I.C Head

Reputation: -1
• Posts: 22
• Joined: 28-February 10

# Temperature Conversion

Posted 06 April 2010 - 07:40 PM

Can someone please help me! I'm doing a loop and I should have 3 options: 1. Fahrenheit to Celsius, 2. Celsius to Fahrenheit, and 3. exit. This is my code

```public class Project2 {
private static int num;

public static void main(String[] args) {
//Create a Scanner
Scanner reader = new Scanner(System.in);

//two variables, undefined.

double usersEnteredTemperature; //the temperature you wish to get from
//the user.
double convertedTemperature;    //the converted temperature based on
//the user choices and entered temperature.

//Enter Input
//Give user options and choices.

int menuChoice;

double celsius;
double fahrenheit;

do {

System.out.println("Enter the number your choice");
System.out.println("1. For Farenheit to Celsius");
System.out.println("2. For Celsius to Farenheit");
System.out.println("3.Exit");
int num = reader.nextInt();

if (num == 1) {
System.out.println("Enter Temperature in Farenheit : ");
usersEnteredTemperature = reader.nextDouble();

//do some conversion based on userinput store output into
//convertedTemperature Celsius
convertedTemperature = (5.0 / 9) * (usersEnteredTemperature - 32);
System.out.println("Your degrees will be in celsius");
System.out.println(convertedTemperature);
} else if (num == 2) {
System.out.println("Enter Temperature in Celsius: ");
usersEnteredTemperature = reader.nextDouble();

//do some conversion based on userinput store output into
//convertedTemperature Fahrenheit
convertedTemperature = (9.0 / 5) * usersEnteredTemperature + 32;
System.out.println("Your degrees will be in Farenheit");
System.out.println(convertedTemperature);
}

} while (num != 3);

System.out.println("Good Bye!");

}
}
```

Help please! This is the error I'm getting.

Quote

run:
Exception in thread "main" java.lang.UnsupportedOperationException: Not yet implemented
at Scanner.<init>(Scanner.java:16)
at Project2.main(Project2.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

Moderator Edit: Please, in the future, .

-Locke

Is This A Good Question/Topic? 0

## Replies To: Temperature Conversion

### #2 pdkharkar

• D.I.C Regular

Reputation: 65
• Posts: 345
• Joined: 19-January 09

## Re: Temperature Conversion

Posted 06 April 2010 - 08:02 PM

you have to import java.util.* package which contains the scanner class
Was This Post Helpful? 0

### #3 LiveToCode

• New D.I.C Head

Reputation: 11
• Posts: 37
• Joined: 17-March 10

## Re: Temperature Conversion

Posted 06 April 2010 - 08:07 PM

You forgot to import java.util.Scanner; and you should replace the variable "num" with "menuChoice" because menuChoice is more decriptive it is declared for a reason right. The variable num in this case is not necessary so you can just delete it.

Edit - Ninja'ed

This post has been edited by LiveToCode: 06 April 2010 - 08:08 PM

Was This Post Helpful? 0

### #4 mothan

• New D.I.C Head

Reputation: -1
• Posts: 22
• Joined: 28-February 10

## Re: Temperature Conversion

Posted 06 April 2010 - 08:34 PM

I've add the import java.util.Scanner; and change the num to menuChoice, here is my code

package project2;

import java.util.Scanner;

public class Project2 {

public static void main(String[] args) {
//Create a Scanner
Scanner reader = new Scanner(System.in);

//two variables, undefined.

double usersEnteredTemperature; //the temperature you wish to get from
//the user.
double convertedTemperature; //the converted temperature based on
//the user choices and entered temperature.

//Enter Input
//Give user options and choices.

int menuChoice;

double celsius;
double fahrenheit;

do {

System.out.println("Enter the number your choice");
System.out.println("1. For Farenheit to Celsius");
System.out.println("2. For Celsius to Farenheit");
System.out.println("3.Exit");
int menuChoice= reader.nextInt();

if (menuChoice == 1) {
System.out.println("Enter Temperature in Farenheit : ");
usersEnteredTemperature = reader.nextDouble();

//do some conversion based on userinput store output into
//convertedTemperature Celsius
convertedTemperature = (5.0 / 9) * (usersEnteredTemperature - 32);
System.out.println("Your degrees will be in celsius");
System.out.println(convertedTemperature);
} else if (menuChoice == 2) {
System.out.println("Enter Temperature in Celsius: ");
usersEnteredTemperature = reader.nextDouble();

//do some conversion based on userinput store output into
//convertedTemperature Fahrenheit
convertedTemperature = (9.0 / 5) * usersEnteredTemperature + 32;
System.out.println("Your degrees will be in Farenheit");
System.out.println(convertedTemperature);
}

} while (menuChoice != 3);

System.out.println("Good Bye!");

}
}

The error I got is this.
run:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - class Project2 is public, should be declared in a file named Project2.java
at project2.Project2.<clinit>(Main.java:11)
Could not find the main class: project2.Project2. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

Help please!
Was This Post Helpful? 0

### #5 MadScientist305

• New D.I.C Head

Reputation: 6
• Posts: 44
• Joined: 26-December 09

## Re: Temperature Conversion

Posted 06 April 2010 - 08:38 PM

Your public class is named Project2 yet the file that contains this code is not named Project2.java. Either rename your class to match your file name, or rename the file to match the class name.
Was This Post Helpful? 0

### #6 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: Temperature Conversion

Posted 06 April 2010 - 08:39 PM

Please use the code tags!

Quote

class Project2 is public, should be declared in a file named Project2.java

Is your class named Project2.java?

Also, since you declare the source to be in the package "project2", it should be in a folder named project2

http://java.sun.com/...agingfiles.html
Was This Post Helpful? 0

### #7 mothan

• New D.I.C Head

Reputation: -1
• Posts: 22
• Joined: 28-February 10

## Re: Temperature Conversion

Posted 06 April 2010 - 09:05 PM

I've clean it up but it won't let me debug or run. please advice!
Was This Post Helpful? 0

### #8 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: Temperature Conversion

Posted 06 April 2010 - 09:10 PM

Could you post your modified code?
Was This Post Helpful? 0

### #9 mothan

• New D.I.C Head

Reputation: -1
• Posts: 22
• Joined: 28-February 10

## Re: Temperature Conversion

Posted 06 April 2010 - 09:12 PM

Here is my code:
```package project2;

import java.util.Scanner;

public class Project2 {

public static void main(String[] args) {
//Create a Scanner
Scanner reader = new Scanner(System.in);

//two variables, undefined.

double usersEnteredTemperature; //the temperature you wish to get from
//the user.
double convertedTemperature;    //the converted temperature based on
//the user choices and entered temperature.

//Enter Input
//Give user options and choices.

int menuChoice = 0;

double celsius;
double fahrenheit;

do {

System.out.println("Enter the number your choice");
System.out.println("1. For Farenheit to Celsius");
System.out.println("2. For Celsius to Farenheit");
System.out.println("3.Exit");

if (menuChoice == 1) {
System.out.println("Enter Temperature in Farenheit : ");
usersEnteredTemperature = reader.nextDouble();

//do some conversion based on userinput store output into
//convertedTemperature Celsius
convertedTemperature = (5.0 / 9) * (usersEnteredTemperature - 32);
System.out.println("Your degrees will be in celsius");
System.out.println(convertedTemperature);
} else if (menuChoice == 2) {
System.out.println("Enter Temperature in Celsius: ");
usersEnteredTemperature = reader.nextDouble();

//do some conversion based on userinput store output into
//convertedTemperature Fahrenheit
convertedTemperature = (9.0 / 5) * usersEnteredTemperature + 32;
System.out.println("Your degrees will be in Farenheit");
System.out.println(convertedTemperature);
}

} while (menuChoice != 3);

System.out.println("Good Bye!");

}
}
```

Edited by Dogstopper: USE THE CODE TAGS!

This post has been edited by Dogstopper: 07 April 2010 - 03:37 AM

Was This Post Helpful? 0

### #10 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: Temperature Conversion

Posted 06 April 2010 - 09:23 PM

Please use the code tags!

Your code compiled fine for me, are you sure you're setting up the folders for the package correctly?

Also, you never accept user input, so there is an infinite loop
Was This Post Helpful? 1

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }