Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Please Help -- Do While Loop continues disregarding boolean statement (http://www.go4expert.com/forums/help-loop-continues-disregarding-t27972/)

Linette411 7Mar2012 12:23

Please Help -- Do While Loop continues disregarding boolean statement
 
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);
        }
   
   
               
    }


virxen 7Mar2012 17:29

Re: Please Help -- Do While Loop continues disregarding boolean statement
 
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 7Mar2012 23:01

Re: Please Help -- Do While Loop continues disregarding boolean statement
 
THANK YOU!!!!! not only have you saved my sanity lol but my grade! I can't thank you enough!


All times are GMT +5.5. The time now is 20:54.