# Temperature Conversion

Page 1 of 1

## 7 Replies - 1147 Views - Last Post: 11 April 2010 - 09:26 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=167393&amp;s=81b326eccb2eab0432ab2c053c9aa73e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mothan

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

# Temperature Conversion

Posted 11 April 2010 - 08:45 AM

Can someone please help me. I'm doing an infinite loop on temperature conversion. I need to allow the user to make a choice at the beginning of the loop. (right after the do) What do I need to add? Here is my source code:

```package project2;

import java.util.Scanner;

public class Project2 {

public static void main(String[] args) {
//Create a Scanner

//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.

double celsius;
double fahrenheit;

do {

System.out.println("1. For Farenheit to Celsius");
System.out.println("2. For Celsius to Farenheit");
System.out.println("3.Exit");

System.out.println("Enter Temperature in Farenheit : ");

//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: ");

//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);
}

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

}
}

```

Admin Edit: Please use code tags when posting your code. Code tags are used like so =>

Thanks,
PsychoCoder

Is This A Good Question/Topic? 0

## Replies To: Temperature Conversion

### #2 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Temperature Conversion

Posted 11 April 2010 - 09:04 AM

It's because you never change the value of menuChoice:

```...
...
...
} else if (menuChoice == 2) {
...
...

```

You need to ask the user for input after you present the menu.

This post has been edited by n8wxs: 11 April 2010 - 09:06 AM

### #3 mothan

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

## Re: Temperature Conversion

Posted 11 April 2010 - 09:05 AM

### #4 mothan

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

## Re: Temperature Conversion

Posted 11 April 2010 - 09:12 AM

Thanks, I'm new at this and still confuse. How can I change the value of menuChoice?

### #5 erik.price

• D.I.C Lover

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

## Re: Temperature Conversion

Posted 11 April 2010 - 09:23 AM

```menuChoice = reader.nextInt();
```

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,399
• Joined: 27-December 08

## Re: Temperature Conversion

Posted 11 April 2010 - 09:24 AM

As to assign menuChoice a new value, use the Scanner object you created to get the consoloe input. Like so: menuChoice = scan.nextInt();. You will want to get input after prompting the user with the menu.

### #7 mothan

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

## Re: Temperature Conversion

Posted 11 April 2010 - 09:25 AM

Thanks, where do I insert this in my code? Is it right after the do.

### #8 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Temperature Conversion

Posted 11 April 2010 - 09:26 AM

You simply take an integer input when you're done displaying the menu!

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

// Changing the value of menu.

// Might want to verify any other thing (say if the user entered
// -1 or 56... Have a "catch-all" statement (else).