Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 loop problem
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: Fri May 12, 2006 5:10 pm   Post subject: 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.

code:

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
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Fri May 12, 2006 6:28 pm   Post subject: (No subject)

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:

code:
while (true)


The loop will loop forever because that condition is always true. You need to change it to this:

code:
boolean running = true;
while (running)


And inside your program you can change that running value to false to exit the program.
cool dude




PostPosted: Fri May 12, 2006 6:39 pm   Post subject: (No subject)

this is wat i did and it doesn't work!

code:

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




PostPosted: Fri May 12, 2006 8:00 pm   Post subject: (No subject)

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 <name>.equals(); It returns true or false, and thus can be used in an if condition statement.
code:

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




PostPosted: Fri May 12, 2006 8:05 pm   Post subject: (No subject)

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




PostPosted: Sat May 13, 2006 8:37 am   Post subject: (No subject)

You can either use break to exit the loop, or return to exit the program.
cool dude




PostPosted: Sat May 13, 2006 8:48 am   Post subject: (No subject)

HellblazerX wrote:
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




PostPosted: Sat May 13, 2006 9:18 am   Post subject: (No subject)

HellblazerX wrote:
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.
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Sat May 13, 2006 9:35 am   Post subject: (No subject)

wtd wrote:
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.
cool dude wrote:
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




PostPosted: Sat May 13, 2006 8:20 pm   Post subject: (No subject)

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
code:

 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




PostPosted: Sat May 13, 2006 8:35 pm   Post subject: (No subject)

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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: