java temperature program while loop in scanner
hello,
i just registered on this site because im have a problem with some coding
im am working on this program that is supposed to take a user input (fahrenheit, celsius, kelvin, or rankine) and give an output (fahrenheit, celsius, kelvin, or rankine).
the problem i am having is with the while loop that is supposed to make this program loop back to the very beginning of the menu and take in inputs and give out outputs again, but when i get to the end where it asks : "Do you want to run this program again? y/n: ", i hit "y" the program doesn't loop to the beginning
Code:
import java.util.Scanner;
public class temperatureConverter
{
public static void main (String [] args)
{
Scanner reader=new Scanner(System.in);
double fahrenheit=0;
double celsius=0;
double rankine=0;
double kelvin=0;
int menu;
String runagn="y";
System.out.println("Welcome to the Temperature Converter Program.");
while(runagn=="y")
{
//menu options
System.out.println("This program converts from...");
System.out.println("1)Fahrenheit to Rankine");
System.out.println("2)Fahrenheit to Celsius");
System.out.println("3)Fahrenheit to Kelvin");
System.out.println("4)Celsius to Kelvin");
System.out.println("5)Celsius to Fahrenheit");
System.out.println("6)Celsius to Rankine");
System.out.println("7)Rankine to Fahrenheit");
System.out.println("8)Rankine to Celsius");
System.out.println("9)Rankine to Kelvin");
System.out.println("10)Kelvin to Celsius");
System.out.println("11)Kelvin to Fahrenheit");
System.out.println("12)Kelvin to Rankine");
System.out.print("Please choose one of the options: ");
menu=reader.nextInt();
//fahrenheit to rankine
if(menu==1)
{
System.out.print("Please enter the amount of degrees in Fahrenheit that you want to convert: ");
fahrenheit=reader.nextDouble();
rankine=fahrenheit+459.67;
System.out.println(fahrenheit + " degrees fahrenheit is equal to " + rankine + " degrees rankine.");
}
//fahrenheit to celsius
else if(menu==2)
{
System.out.print("Please enter the amount of degrees in Fahrenheit that you want to convert: ");
fahrenheit=reader.nextDouble();
celsius=(fahrenheit-32.0)*(5.0/9.0);
System.out.println(fahrenheit + " degrees fahrenheit is equal to " + celsius + " degrees celsius.");
}
//fahrenheit to kelvin
else if(menu==3)
{
System.out.print("Please enter the amount of degrees in Fahrenheit that you want to convert: ");
fahrenheit=reader.nextDouble();
kelvin=(fahrenheit+459.67)*5.0/9.0;
System.out.println(fahrenheit + " degrees fahrenheit is equal to " + kelvin + " degrees kelvin.");
}
//celsius to kelvin
else if(menu==4)
{
System.out.print("Please enter the amount of degrees in celsius that you want to convert: ");
celsius=reader.nextDouble();
kelvin=celsius+273.15;
System.out.println(celsius + " degrees celsius is equal to " + kelvin + " degrees kelvin.");
}
//celsius to fahrenheit
else if(menu==5)
{
System.out.print("Please enter the amount of degrees in celsius that you want to convert: ");
celsius=reader.nextDouble();
fahrenheit=celsius*9.0/5.0+32.0;
System.out.println(celsius + " degrees celsius is equal to " + fahrenheit + " degrees fahrenheit.");
}
//celsius to rankine
else if(menu==6)
{
System.out.print("Please enter the amount of degrees in celsius that you want to convert: ");
celsius=reader.nextDouble();
rankine=(celsius+273.15)*9.0/5.0;
System.out.println(celsius + " degrees celsius is equal to " + rankine + " degrees rankine.");
}
//rankine to fahrenheit
else if(menu==7)
{
System.out.print("Please enter the amount of degrees in rankine that you want to convert: ");
rankine=reader.nextDouble();
fahrenheit=rankine-459.67;
System.out.println(rankine + " degrees rankine is equal to " + fahrenheit + " degrees fahrenheit.");
}
//rankine to celsius
else if(menu==8)
{
System.out.print("Please enter the amount of degrees in rankine that you want to convert: ");
rankine=reader.nextDouble();
celsius=(rankine-491.67)*5/9;
System.out.println(rankine + " degrees rankine is equal to " + celsius + " degrees celsius.");
}
//rankine to kelvin
else if(menu==9)
{
System.out.print("Please enter the amount of degrees in rankine that you want to convert: ");
rankine=reader.nextDouble();
kelvin=rankine*5/9;
System.out.println(rankine + " degrees rankine is equal to " + kelvin + " degrees kelvin.");
}
//kelvin to celsius
else if(menu==10)
{
System.out.print("Please enter the amount of degrees in kelvin that you want to convert: ");
kelvin=reader.nextDouble();
celsius=kelvin-273.15;
System.out.println(kelvin + " degrees kelvin is equal to " + celsius + " degrees celsius.");
}
//kelvin to fahrenheit
else if(menu==11)
{
System.out.print("Please enter the amount of degrees in kelvin that you want to convert: ");
kelvin=reader.nextDouble();
fahrenheit=kelvin*9.0/5.0-459.67;
System.out.println(kelvin + " degrees kelvin is equal to " + fahrenheit + " degrees fahrenheit.");
}
//kelvin to rankine
else if(menu==12)
{
System.out.print("Please enter the amount of degrees in kelvin that you want to convert: ");
kelvin=reader.nextDouble();
rankine=kelvin*9.0/5.0;
System.out.println(kelvin + " degrees kelvin is equal to " + rankine + " degrees rankine.");
}
System.out.print("Do you want to run this program again? y/n: ");
runagn=reader.next();
//just to check if y or n actually gets assigned runagn
System.out.println(runagn);
}
System.out.println("Thank You for using this temperature converting program.");
}
}
sorry if the mistake i made might seem really stupid but ive been using TerminalIO.KeyboardReader for as long as ive been programming in java and ive never used scanner before so i know nothing about it
Re: java temperature program while loop in scanner
You state that you're having problems but haven't told us what the problems are. You may wish to clarify this. Also please edit your post above and place [code] [/code] tags around your code so that the code is readable.
Re: java temperature program while loop in scanner
TerminalIO is pretty awful, and has a lot of platform to platform issues. Scanner probably is the way to go. That being said, what is printed on line 151 when it fails to loop?
Re: java temperature program while loop in scanner
Look at your check in the while statement. Is that how you compare Strings?
Re: java temperature program while loop in scanner
I missed that, good point. Groovy has spoiled me!
Re: java temperature program while loop in scanner
Well, y does get assigned to scanner and this is my very first program with scanner so i honestly dont know what to do
Re: java temperature program while loop in scanner
Quote:
Originally Posted by
sauronamaterasu13
Well, y does get assigned to scanner and this is my very first program with scanner so i honestly dont know what to do
This statement is a bit confusing to me. Please clarify your question. Assume we are 6 year old idiots and explain it to us as if we know nothing about your problem.
Re: java temperature program while loop in scanner
Basically i cant get the while loop to loop when i hit y
Wether i hit y or n at the end of the loop it will just end the loop
Re: java temperature program while loop in scanner
One problem that you're having has been mentioned tangentially above: you're using == to compare Strings and shouldn't be doing this. == checks if one *object* is the same as another, and this isn't what you want to do. Instead you want to see if one String object holds the same characters in the same order as another String object, and for that you'd use String's equals(...) or equalsIgnoreCase(...) methods.
For example, not:
Code:
while (runagn=="y") {
// ...
}
but rather:
Code:
while ("y".equalsIgnoreCase(runagn)) {
// ...
}
Re: java temperature program while loop in scanner
Quote:
Originally Posted by
Fubarable
Assume we are 6 year old idiots
Well, I'm not six years old but the rest of the description is quite correct.
kind regards,
Jos ;-)