Results 1 to 10 of 10
Like Tree3Likes
  • 1 Post By StormyWaters
  • 2 Post By JosAH

Thread: java temperature program while loop in scanner

  1. #1
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default 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

    Java 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
    Last edited by sauronamaterasu13; 08-24-2012 at 05:54 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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?

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default Re: java temperature program while loop in scanner

    Look at your check in the while statement. Is that how you compare Strings?
    quad64bit likes this.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: java temperature program while loop in scanner

    I missed that, good point. Groovy has spoiled me!

  6. #6
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default 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

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: java temperature program while loop in scanner

    Quote Originally Posted by sauronamaterasu13 View Post
    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.

  8. #8
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default 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

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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:

    Java Code:
    while (runagn=="y") {
       // ...
    }
    but rather:

    Java Code:
    while ("y".equalsIgnoreCase(runagn)) {
       // ...
    }

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: java temperature program while loop in scanner

    Quote Originally Posted by Fubarable View Post
    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 ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 6
    Last Post: 05-01-2011, 10:42 PM
  2. Temperature analysis program
    By Evii0 in forum New To Java
    Replies: 8
    Last Post: 03-29-2011, 09:05 AM
  3. Reading a temperature sensor in Java
    By michaelwest in forum New To Java
    Replies: 2
    Last Post: 02-23-2010, 03:59 AM
  4. Scanner-While Loop
    By hyunski in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 02:15 AM
  5. temperature program with inputs
    By JingGong in forum New To Java
    Replies: 3
    Last Post: 10-17-2008, 06:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •