1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Please Help -- Do While Loop continues disregarding boolean statement

Discussion in 'Java' started by Linette411, Mar 7, 2012.

  1. Linette411

    Linette411 New Member

    Joined:
    Mar 7, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I am a beginner in programming and for this assignment (for an online class) I cannot seem to figure out how to fix/what I am doing wrong and since its an online class I really cant ask for help.Please help! So in my code I would like the user to enter a scale either 'c' or 'f'....now if you enter anything else it catches the error but then still proceeds! also, when rounding the calculated conversion...i am not getting a rounded answer only **.00 the asterisks being any numeral. here is my code:
    Code:
    package temperatureconversion;
    import java.util.Scanner;
    import java.text.DecimalFormat;
    /**
     *
     * @author Linette
     */
    public class TemperatureConversion {
    
        public static void main(String[] args) {
           
            System.out.println("\t\t\tHello " + System.getProperty("user.name") + "!");
            String os = System.getProperty("os.name");
            String version = System.getProperty("os.version");
            System.out.println("\tI currently am running "+os+", version "+version);
                    
            boolean correctScale = true;
            Scanner keyboard = new Scanner(System.in);
            int tempPicked;
            
            System.out.println("Enter a temperature scale: 'F' for fahrenheit or 'C' for celsius)");
            String scaleType = keyboard.nextLine();
            scaleType = scaleType.toUpperCase();
            char answerScale  = scaleType.charAt(0);
           
            do
            {
               if (answerScale != 'F' && answerScale != 'f' && answerScale != 'C' && answerScale != 'c')
                System.out.println("Error: Scale not found please enter 'F' for Fahrenheit or 'C' for Celsius");
               else
                 System.out.println("You have choosen the '" + answerScale + "' scale."); 
             
             System.out.println("Please enter a temperature (whole integer,no fractions)");
                tempPicked = keyboard.nextInt();
       
            Scanner scan = new Scanner(System.in);
            double convertedCelsius, convertedFahrenheit; 
        
                DecimalFormat formatter=new DecimalFormat("0.00");
                switch (answerScale)
                {
                    case 'f':
                        convertedCelsius = (5*(tempPicked-32))/9;
                        System.out.println("Converts to = ");
                        Double.toString(convertedCelsius);
                        System.out.println(formatter.format(convertedCelsius)+ " degrees Celsius");
                       break;
                    case 'F':
                        convertedCelsius = (5*(tempPicked-32))/9;
                        System.out.println("Converts to = ");
                        Double.toString(convertedCelsius);
                        System.out.println(formatter.format(convertedCelsius)+ " degrees Celsius");
                        break;
                    case 'C':
                        convertedFahrenheit = (9*tempPicked)/5 +32;
                        System.out.println("Converts to = ");
                        Double.toString(convertedFahrenheit);
                        System.out.println(formatter.format(convertedFahrenheit)+ " degrees Fahrenheit");
                        break;
                    case 'c':
                        convertedFahrenheit = (9*tempPicked)/5 +32;
                        System.out.println("Converts to = ");
                        Double.toString(convertedFahrenheit);
                        System.out.println(formatter.format(convertedFahrenheit)+ " degrees Fahrenheit");
                        break;
                    default:
                        System.out.println("Please enter a valid character to set scale:");
                        break;
                }
                 }  while (correctScale = false);
            }
         
         
                    
        }
     
    Last edited by a moderator: Mar 7, 2012
  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    try this
    Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    /**
     *
     * @author Linette
     */
    public class TemperatureConversion {
    
        public static void main(String[] args) {
           
            System.out.println("\t\t\tHello " + System.getProperty("user.name") + "!");
            String os = System.getProperty("os.name");
            String version = System.getProperty("os.version");
            System.out.println("\tI currently am running "+os+", version "+version);
                    
            boolean correctScale = true;
            Scanner keyboard = new Scanner(System.in);
            int tempPicked;
            String scaleType="";
            char answerScale=' ';
            System.out.println("Enter a temperature scale: 'F' for fahrenheit or 'C' for celsius)");
            do{
            	
            	scaleType = keyboard.nextLine();
            	scaleType = scaleType.toUpperCase();
                answerScale  = scaleType.charAt(0);
                if (answerScale != 'F' && answerScale != 'f' && answerScale != 'C' && answerScale != 'c'){
                	System.out.println("Error: Scale not found please enter 'F' for Fahrenheit or 'C' for Celsius");
                	correctScale=false;
                }else
    				correctScale=true;
            }while (!correctScale);
            System.out.println("You have choosen the '" + answerScale + "' scale.");
            	System.out.println("Please enter a temperature (whole integer,no fractions)");
                tempPicked = keyboard.nextInt();
                //Scanner scan = new Scanner(System.in);
            	double convertedCelsius, convertedFahrenheit; 
                DecimalFormat formatter=new DecimalFormat("0.00");
                switch (answerScale){
                    case 'f':
                    case 'F':
                        convertedCelsius = (5.0*(tempPicked-32.0))/9.0;
                        System.out.println("Converts to = ");
                        Double.toString(convertedCelsius);
                        System.out.println(formatter.format(convertedCelsius)+ " degrees Celsius");
                        break;
                    case 'C':
                    case 'c':
                        convertedFahrenheit = (9.0*tempPicked)/5.0 +32.0;
                        System.out.println("Converts to = ");
                        Double.toString(convertedFahrenheit);
                        System.out.println(formatter.format(convertedFahrenheit)+ " degrees Fahrenheit");
                        break;
                    default:
                        System.out.println("Please enter a valid character to set scale:");
                        break;
                }
            }
         
         
                    
        }
    
    
    
     
    Linette411 likes this.
  3. Linette411

    Linette411 New Member

    Joined:
    Mar 7, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    THANK YOU!!!!! not only have you saved my sanity lol but my grade! I can't thank you enough!
     

Share This Page