Joseph Reed
Joseph Reed

Reputation: 73

New to java, while loop?

I have a code here that is supposed to convert Celsius to Fahrenheit and vice versa, and then ask the user if they want to run the program again. However there is a boundary that that user can only enter a number that ranges from -100 - 100. If the user enters a number greater or less than the range, it has to ask the user to enter a new number. I'm having trouble with the program ending and asking the user to rerun the program after they enter a correct number. Does anyone have any suggestions? Thanks!

import javax.swing.JOptionPane;
public class Temp3 {

        public static void main(String[] args) {
            // String sentence = "This is a Java program for converting
            // temperatures.";

            float option = JOptionPane.YES_OPTION;

            while (option == JOptionPane.YES_OPTION) {
                option = JOptionPane.showConfirmDialog(null, "Would you like to convert from Celsius to Fahrenheit?");

                if (option == JOptionPane.YES_OPTION) {
                    c2f();
                }

                else if (option == JOptionPane.NO_OPTION) {
                    f2c();
                }

                else {
                    JOptionPane.showMessageDialog(null, "Goodbye");
                    System.exit(0);
                }

                option = JOptionPane.showConfirmDialog(null, "Would you like to run the program again?");
                }// while loop

        if (option == JOptionPane.NO_OPTION) {
            JOptionPane.showMessageDialog(null, "Goodbye");
        } // main method
    }
        private static void c2f() {
            String celVal = JOptionPane.showInputDialog("Please enter degrees in Celsius. ");
            float cel = Float.parseFloat(celVal);

            while (cel <= 100 && cel >= -100) {
                double holder = ((cel * 1.8) + 32);
                JOptionPane.showMessageDialog(null, "The temperature in Fahrenheit is " + holder);
                break;
            }

            while (cel > 100) {

                String celVal2 = JOptionPane.showInputDialog("That number is too high. Please enter a number lower than 100 and try again.");
                float cel2 = Float.parseFloat(celVal2);

            while (cel2 <= 100 && cel2 >= -100){
                JOptionPane.showMessageDialog(null, "The temperature in Fahrenheit is " + ((cel2 * 1.8) + 32));
                cel2 = 101;

                }

            } 
            }



        // cel to fah

        private static void f2c() {
            String fahVal = JOptionPane.showInputDialog(
                    "You are now converting from Fahrenheit to Celsius. Please enter degrees in Fahrenheit.");
            float fah = Float.parseFloat(fahVal);
            while (fah < 100 && fah >-100){
                JOptionPane.showMessageDialog(null, "The temperature in Celcius is " + ((fah - 32) / 1.8));
                break;
            }
            if (fah > 100) {
                JOptionPane.showMessageDialog(null,
                        "That number is too high. Please enter a temperature less than 100 and try again.");
            }

            else if (fah < -100) {
                JOptionPane.showMessageDialog(null,
                        "That number is too low. Please enter a temperature more than -100 and try again.");


            }


        } // fah to cel

Upvotes: 0

Views: 463

Answers (2)

triForce420
triForce420

Reputation: 729

This is a good solution:

import javax.swing.JOptionPane;
public class Temp3 {

    public static void main(String[] args) {
            // String sentence = "This is a Java program for converting
            // temperatures.";
            String [] options = {"Convert from Celsius to fahrenheit", "Convert from Fahrenheit to Celsios","Exit"};
            int choice=0;
            do{ //do while is used so that the loop will run AT LEAST ONCE
                 choice = JOptionPane.showOptionDialog(null,"What do you want to do?", 
                        "Choose an option", JOptionPane.YES_NO_CANCEL_OPTION, //option type 
                        JOptionPane.QUESTION_MESSAGE, 
                        null, options, options[0]);
                 convertTemp(choice);
            }while(choice!=2);

    }

    private static void convertTemp(int choice) {
        // TODO Auto-generated method stub
        String from = "", to = "";
        if(choice==0){ //set to and form so that you don't need to type the units again and again 
            from="Celsius"; 
            to ="Fahrenheit";
        }else if(choice==1){
            to="Celsius"; 
            from ="Fahrenheit";
        }else{
            JOptionPane.showMessageDialog(null,"Goodbye","Exit",JOptionPane.INFORMATION_MESSAGE);
            //I often put message type to make it more presentable
            return; //exit method :) 
        }
        Float tempFrom= Float.parseFloat(JOptionPane.
                showInputDialog("You are now converting from "+from+" to "+ to+"."
                        + "Please enter degrees in "+from +".")); //get input 
        while(tempFrom<-100 ||tempFrom>100){ //check if input is valid 
            if(tempFrom>100){
                tempFrom = Float.parseFloat(JOptionPane.showInputDialog(null, "That number is too high. "
                        + "Please enter a temperature between -100 & 100 degress "+from , "Temperature too high"
                        , JOptionPane.ERROR_MESSAGE));//inform the user about the error and ask for the input at the same time 
            }else{
                tempFrom = Float.parseFloat(JOptionPane.showInputDialog(null, "That number is too low. "
                        + "Please enter a temperature between -100 & 100 degress "+from , "Temperature too low"
                        , JOptionPane.ERROR_MESSAGE));//inform the user about the error and ask for the input at the same time
            }
        }
            double answer =0;
            if(choice==0){ //celsius to fahrenheit 
                answer = (tempFrom * 1.8) + 32;
            }else{ //fahrenheit to celsius 
                answer = (tempFrom- 32) / 1.8;
            }
            JOptionPane.showMessageDialog(null, "The temperature in "+ to +" is " +
                    answer  
                    ,to,
                    JOptionPane.INFORMATION_MESSAGE);
    }
}

Basically I used showOptionDialog instead of showConfirmDialog so that I can edit the caption of the buttons . We need to run the application at least once, so do while loop is perfect.

Upvotes: 1

Neil Locketz
Neil Locketz

Reputation: 4318

For your conversion functions I would recommend saying while(enteredNum is out of range) instead of saying while the number is within range as that doesnt really tell you anything. kind of thing and keep asking. Then you can easily do the conversion and return

Upvotes: 1

Related Questions