Hack Forums
[Java] Repeat first while loop if condition in second while loop is true - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Repeat first while loop if condition in second while loop is true (/showthread.php?tid=5708656)



Repeat first while loop if condition in second while loop is true - prodigyhf - Sep 1, 2017

Hey guys. Trying to figure out how to get the first while loop to repeat if the if statement inside the second while loop is true.

Code
import java.util.Scanner;
import java.text.*;

public class TemperatureConversion
{
    public static void main(String[] args)
    {
        System.out.println("Welcome to the Temperature Converter\n");
        Scanner scChoice = new Scanner(System.in);
        Scanner scTemp = new Scanner(System.in);
        Scanner scCont = new Scanner(System.in);
        String choice;
        String cont;
        double finalTemp;
        double fahrenheit;
        double celsius;
        boolean loop = true;
        boolean contAsk = false;
        DecimalFormat df = new DecimalFormat("###,###.##");
        
        System.out.print("Would you like to convert Fahrenheit or Celsius? (F/C):");
        choice = scChoice.next();

        while(loop = true){
                        
            if(choice.equalsIgnoreCase("f")){
                System.out.print("Enter the temperature:");
                fahrenheit = scTemp.nextDouble();
                finalTemp = (fahrenheit - 32) * .5556;
                System.out.print(fahrenheit + " degrees Fahrenheit is " + df.format(finalTemp) + " degrees Celsius.\n");
                contAsk = true;
                loop = false;
                break;
            }else if (choice.equalsIgnoreCase("c")){
                System.out.print("Enter the temperature:");
                celsius = scTemp.nextDouble();
                finalTemp = 32 + (celsius * 1.8);
                System.out.print(celsius + " Celsius is " + df.format(finalTemp) + " degrees Fahrenheit.\n");
                contAsk = true;
                loop = false;
                break;
            }else if (!choice.equalsIgnoreCase("f") && !choice.equalsIgnoreCase("c")){
                System.out.print("Error: invalid entry\n");
                System.out.print("Would you like to convert Fahrenheit or Celsius? (F/C):");
                choice = scChoice.next();
                }
            }
        
            System.out.println("Continue? (Y/N):");
            cont = scCont.next();
        
        while(contAsk = true){
                
            if(cont.equalsIgnoreCase("y"))
            {
                loop = true;
            }else if (!cont.equalsIgnoreCase("y") && !cont.equalsIgnoreCase("n")){
                System.out.println("Error: invalid entry\n");
                System.out.println("Continue? (Y/N):");
                cont = scCont.next();  
            }else if (cont.equalsIgnoreCase("n"))
                return;
            }
        }
}