
-----------------------------------
cool dude
Fri May 12, 2006 5:10 pm

loop problem
-----------------------------------
i'm trying to convert fareinheight to celcius and it works, but the problem i'm having is that it only does it once. i'm trying to put the code in a loop but then it starts looping forever and i can't exit the program. 


import java.io.*;

public class Temperature{
    public static void main(String[] args) {
        String sfahrenheit;
        double dfahrenheit;
        double celcius;
        String ans = "";
        
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter a Fahrenheit degree");
            sfahrenheit = in.readLine();
            dfahrenheit = Double.parseDouble(sfahrenheit);
            celcius = (dfahrenheit - 32)/1.8;
            System.out.println(dfahrenheit + " fahrenheit" + " = " + celcius + " Celcius");
        }catch(NumberFormatException nfe){
            System.out.println("Can't convert");
        }catch(IOException io){
            System.out.println("error reading input");
        }
    }
}

P.S. when it starts looping forever how can i force it to stop. the "x" at the top is disabled and if i click on file close the window will close but its still running in my task manager and if i try to compile my code again it won't let me. i have to close the program and restart it to stop executing

-----------------------------------
HellblazerX
Fri May 12, 2006 6:28 pm


-----------------------------------
One way is to use the return method.  By calling the return function, you cause your main method to stop running, thus stopping your program.  However, there are no infinite loops in Java, only while loops and for loops.  While loops will continue to loop as long a condition is met.  In your case, you probably had it like this:

while (true)

The loop will loop forever because that condition is always true.  You need to change it to this:

boolean running = true;
while (running)


And inside your program you can change that running value to false to exit the program.

-----------------------------------
cool dude
Fri May 12, 2006 6:39 pm


-----------------------------------
this is wat i did and it doesn't work!


import java.io.*;

public class Temperature{
    public static void main(String[] args) {
        String sfahrenheit;
        double dfahrenheit;
        double celcius;
        String ans = "";
        boolean running = true; 
        
        while (running) 
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter a Fahrenheit degree");
            sfahrenheit = in.readLine();
            dfahrenheit = Double.parseDouble(sfahrenheit);
            celcius = (dfahrenheit - 32)/1.8;
            System.out.println(dfahrenheit + " fahrenheit" + " = " + celcius + " Celcius");
            System.out.println("do you have another fahrenheit degree?");
            ans = in.readLine();
            if (ans == "no"){
                running = false;
            }
        }catch(NumberFormatException nfe){
            System.out.println("Can't convert");
        }catch(IOException io){
            System.out.println("error reading input");
        }
    }
}

-----------------------------------
Krabjuice
Fri May 12, 2006 8:00 pm


-----------------------------------
I see the nature of your problem.  Your string comparison at the if isn't right.  Also, you can use the break; command.

With strings, use .equals();  It returns true or false, and thus can be used in an if condition statement.

import java.io.*;

public class Temperature{
    public static void main(String[] args) {
        String sfahrenheit;
        double dfahrenheit;
        double celcius;
        String ans = "";
       
        while (true)
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter a Fahrenheit degree");
            sfahrenheit = in.readLine();
            dfahrenheit = Double.parseDouble(sfahrenheit);
            celcius = (dfahrenheit - 32)/1.8;
            System.out.println(dfahrenheit + " fahrenheit" + " = " + celcius + " Celcius");
            System.out.println("do you have another fahrenheit degree?");
            ans = in.readLine();
            if (ans.equals( "no")){
                break;
            }
        }catch(NumberFormatException nfe){
            System.out.println("Can't convert");
        }catch(IOException io){
            System.out.println("error reading input");
        }
    }
}

-----------------------------------
cool dude
Fri May 12, 2006 8:05 pm


-----------------------------------
thanks i totally forgot to use string in if statements u have to say name.equals(). also do u know how to exit an infinite loop, because if i go to file-close it will close the window but it will still be executing and as a result i can't compile the program or run it again unless i close BlueJ and reopen it.

-----------------------------------
HellblazerX
Sat May 13, 2006 8:37 am


-----------------------------------
You can either use break to exit the loop, or return to exit the program.

-----------------------------------
cool dude
Sat May 13, 2006 8:48 am


-----------------------------------
You can either use break to exit the loop, or return to exit the program.

no i'm talking about when your already running the program! how can u exit the program while its running?

-----------------------------------
wtd
Sat May 13, 2006 9:18 am


-----------------------------------
You can either use break to exit the loop, or return to exit the program.

You're oversimplifying.  "break" will cut out of the innermost control structure.  "return" will transfer control out of the current method.

-----------------------------------
HellblazerX
Sat May 13, 2006 9:35 am


-----------------------------------
You're oversimplifying.  "break" will cut out of the innermost control structure.  "return" will transfer control out of the current method.
Right, sorry forgot about that.  Ya, break can be used for structures other than loops.
no i'm talking about when your already running the program! how can u exit the program while its running?
return would work if you're using it inside you main method, because if will cause your main method to stop, and therefore the program will stop as well.  I guess you could also use System.exit (0).

-----------------------------------
cool dude
Sat May 13, 2006 8:20 pm


-----------------------------------
k i don't think your understanding my question. if i did not put return or system.exit(0) or break in my code and i ran the program and i get stuck in an endless loop. how do i exit the program while its running. 

for example: take my code and comment out 

 if (ans.equals( "no")){ 
                break; 
            } 


then compile the code and run it. how r u going to end it from running. it keeps asking you to enter another fareinheight degree

-----------------------------------
HellblazerX
Sat May 13, 2006 8:35 pm


-----------------------------------
You can't.  You'll have to exit your editor program, or whatever that is running the program, because there's nothing telling Java to exit.  That's why you put those exit conditions in, so you don't run into this problem.
