# Loops and display problem

• 02-26-2010, 03:48 PM
lk1001
Loops and display problem
Hey all,

so new java developer here and I am trying to create a program that will show this to the user.

Please enter the temperature you would like to convert - 0
Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2
0.00000 degrees Celsius = 32.000 degrees Fahrenheit.
Would you like to continue? ( 0 = No ; 1 = Yes ) - 1
Please enter the temperature you would like to convert – 32.0
Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 1
32.000 degrees Fahrenheit = 0.00000 degrees Celsius.
Would you like to continue? ( 0 = No ; 1 = Yes ) – 0.0
Good Bye!

I have hit some road blocks in my development and I was hoping someone could point me in the right direction.

As this is a school project, I would rather do it myself than have someone rewrite all my code. Thanks

Here is my code ( I havent modified the output to match the required output as I was just plugging things in first) Also for this I need celcius to have 5 decimal points and Fahrenheit to have 3)

mport java.util.Scanner;

public class Lab{

public static void main (String [] args){

Scanner input = new Scanner(System.in);
{
double celsius;
double farenheit;
int degrees;

System.out.print("Please enter the temperature you would like to convert : ");
double temp = input.nextDouble ();

char response = 0;
while (response == 'Y')
{
System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2");
degrees = input.nextInt ();

if((degrees == '1') || (degrees == '1'))
{
farenheit = 9/5 * (temp) + 32;
System.out.println("Temperature = " +temp + 'C');
System.out.println("Temperature in Farenheit = " +farenheit +'F');
}

else if ((degrees == 'F') || (degrees == 'f'))
{
celsius = 5/9 * (temp - 32);
System.out.println("Temperature = " +temp + '1');
System.out.println("Temperature in Celsius = " +celsius + '2');
}
else
{
System.out.println("Good Bye");
//System.out.println("You entered" +degrees);
System.out.println("Enter 1 for Celsius or 2 for Farenheit");
System.out.print("Enter temperature: ");
temp = input.nextDouble ();

}

System.out.print("Would you like to enter another temperature? Y or N: ");

//System.out.print("Enter <strong class="highlight">a</strong> temperature: ");

}

}
} // End Main
} // End Class DegreesConverter
• 02-26-2010, 04:21 PM
FlyNn
Hey, I 've ust tried to run your code and even I am stuck. Im not sure what is meant to happen. No matter what value you enter the program just quits. :confused:

Seems that you are having some confusion with your variables too: You ask the user for an input and its just "fluffy".
Code:

`double temp = input.nextDouble ();`
what is the purpose of temp?

I think the main problem is that you use three variables. why dont you just ask the user to select 1 for F and 0 for C.Then depending of the selection enter temperature in F and convert it to C and vice versa and get rid of variable "degrees".

This looks interesting too:
Code:

```System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2"); degrees = input.nextInt (); if((degrees == '1') || (degrees == '1')) { farenheit = 9/5 * (temp) + 32; System.out.println("Temperature = " +temp + 'C'); System.out.println("Temperature in Farenheit = " +farenheit +'F'); } else if ((degrees == 'F') || (degrees == 'f')) { celsius = 5/9 * (temp - 32); System.out.println("Temperature = " +temp + '1'); System.out.println("Temperature in Celsius = " +celsius + '2'); }```
Seems like your code expects the user to enter "1" or "F" :confused: while you asked the user to input 1 or 2. It may be just a typo. I dont know. Plus if you declare a char variable, java understands the value as ASCII character - so it converts it to a decimal value(sort off). I guess it would be easier if you declare it as String instead of char.

Seems like the code is too complicated and "fluffy" for the intended purpose. If you have some sort of specification that you are following that would be interesting to see too.
• 02-26-2010, 04:33 PM
lk1001

I too am having trouble running the program. I made some changes as best I know how based on your reply, but the program will terminate after the first line.

What I am trying to build is a program that will ask the user for a temp

Store that

Ask the user to input 1 for Fahrenheit and 2 for Celsius

(Use five decimal places for Fahrenheight and three for Celsius)

Calculate the temp based on user input

Display User input and calculation (32.000 degrees Fahrenheit = 0.00000 degrees Celsius.)

Ask the user if they want to calculate another temp (Y or N)

Y restarts the program while N ends with good bye

Here are my modifications, any help would be greatly appreciated as I am beyond lost now.

import java.util.Scanner;

public class Lab4{

public static void main (String [] args){

Scanner input = new Scanner(System.in);
{
double celsius;
double farenheit;
int degrees;

System.out.print("Please enter the temperature you would like to convert : ");
double temp = input.nextDouble ();

char response = 0;
while (response == 'Y')
{
System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2");
degrees = input.nextInt ();

if((degrees == '1') || (degrees == '1'))
{
farenheit = 9/5 * (temp) + 32;
System.out.println("Temperature = " +temp + '1');
System.out.println("Temperature in Farenheit = " +farenheit +'1');
}

else if ((degrees == '2') || (degrees == '2'))
{
celsius = 5/9 * (temp - 32);
System.out.println("Temperature = " +temp + '1');
System.out.println("Temperature in Celsius = " +celsius + '2');
}
else
{

System.out.println("Enter 1 for Celsius or 2 for Farenheit");
System.out.print("Enter temperature: ");
temp = input.nextDouble ();

}

System.out.print("Would you like to enter another temperature? Y or N: ");

if (response == 'N')
System.out.println("Good Bye");
//System.out.println("Good Bye);

//System.out.print("Enter <strong class="highlight">a</strong> temperature: ");

}

}
} // End Main
} // End Class DegreesConverter
• 02-26-2010, 04:46 PM
FlyNn
Quote:

What I am trying to build is a program that will ask the user for a temp
<variable name> = ScannerInput; <-- you automatically store it in a <variable name>

Quote:

Ask the user to input 1 for Fahrenheit and 2 for Celsius
ok so this time you need a different <variable name1> to store 1 or 2. I guess int would do. What do you think? So its a simple if statement.

If <variable name1> == something
{
DO THIS and display output
}
else if <variable name1> == something
{
DO THIS and display output
}

Quote:

Ask the user if they want to calculate another temp (Y or N)
This will require a loop. So the idea is that: while <variable name> is Y run the code. Hint: loop will have to cover the whole code (if you know what i mean).
• 02-26-2010, 05:02 PM
lk1001
First of all, thank you so much for your help.

Secondly,

Is this a good start to looping the whole thing together?

import java.util.Scanner;

public class Lab4{

public static void main (String [] args){

Scanner input = new Scanner(System.in);
{
char Result;
int Degrees;
int decision;

if decision ==1{

System.out.print("Good Bye")

else decision == 0{

System.out.print("Please enter the temperature you would like to convert - ");
Temp = input.nextDouble();
• 02-26-2010, 05:13 PM
FlyNn
Well you havent looped anything yet. If statements do not loop. Read about While, Do Until, Do While, For loops.

Code:

```import java.util.Scanner; public class Lab4{ public static void main (String [] args) {     <someVariable> = 1;     Scanner input = new Scanner(System.in);     <someVariable = input;       while{<someVariable>==1)   {     //run your code   }```
• 02-26-2010, 05:26 PM
lk1001
Again thank you so much for all of your help

I have reread the areas on loops, and I know I have some issues here, but any chance you could tell me what is wrong here?

import java.util.Scanner;

public class Lab4{

private static double farenheit;
private static double celsius;

public static void main (String [] args){

Scanner input = new Scanner(System.in);
{
int Degrees;
int decision = 0;
double Temp;

System.out.print("Please enter the temperature you would like to convert - ");
Temp = input.nextDouble();

System.out.print("Enter 1 if the temperature you entered is in degrees Fahrenheit and 2 if it is in degrees Celsius - 2");
Degrees = input.nextInt ();

if((Degrees == '1') || (Degrees == '1')){{
farenheit = 9/5 * (Temp) + 32;
System.out.println("(Temp) + Degrees Celsius = (farenheight) + degrees Farenheight");
System.out.print("Would you like to continue? ( 0 = No ; 1 = Yes ) – 0.0");}}

else if(Degrees =='0');
celsius = 5/9 * (Temp - 32);
System.out.println("(Temp) degrees Farenheight = (celsius) Degrees Celsius");}
System.out.print("Would you like to continue? ( 0 = No ; 1 = Yes ) – 0.0");
int decision = (int) input.nextDouble();}

{

char decision = 0;
while (decision =='1'){

System.out.print("Good Bye");

if(decision =='0'){