1

I was wondering if I could make it so a loop can end with typing in a command like end or -1. What I have now is

while ( gradeCounter <= 10 ) // loop 10 times

I would like to know how to have it loop infinitely till I end it.

7
  • 1
    End it asynchronously? Commented Oct 1, 2013 at 19:46
  • Use a boolean variable in the conditional statement and set it to false when you want to break out of the loop. Commented Oct 1, 2013 at 19:46
  • Make the condition to be false and it will break. If you want it to be a console input you can research on Scanner usage and compare the String/input to break the condition. Commented Oct 1, 2013 at 19:47
  • Hi, I am like very new to java and I am not sure how to use boolean Commented Oct 1, 2013 at 19:47
  • @Tom set it to false * Commented Oct 1, 2013 at 19:47

3 Answers 3

2

Simply create a while loop and a condition to break it

while(true){
      String inputString = //Get user input here       

      if(inputString.equals("Your Termination Text"))
           break;

    }

If you want to know how to get console input here is one method Java: How to get input from System.console()

Edit per comments below

double grades = 0;
int entries = 0;
while(true){
      String inputString = //Get user input here       
 
      if(inputString.equals("Your Termination Text"))
           break;
      else{
        grades += Double.parseDouble(inputString);
        entries++;
      }
 }
 double averageGrade = grades / entries;

Please keep in mind that this does not account for text that is not a number and also not your termination text. From the question it sounds like a low level CIS class and I don't think this will be an issue. If it is however you need to learn how to do try catch and some more input validation.

Sign up to request clarification or add additional context in comments.

15 Comments

It's worth mention that the same thing can be done with all of the loops. Put the above loop body into for(;;){//loop body from above} or do{//loop body from above}while(true); Just a matter of personal preference.
If I have average = total / 10 How do I do average for infinite loop?
If you need to average, the for loop is actually pretty good method. for(int i=0;;i++){//find sum, divide by i}
You can also use a less clean but just as effective counter declared outside of your loop then just increment it each iteration with counter++
@SamYonnou That concept is probably a bit to advanced for someone who would even post this original question...
|
1

While(true) {} creates an infinite loop. A break command inside the loop breaks out of it. You'll have to detect whatever sort of event will occur inside the loop to determine if you should break.

I don't know what you're doing on a larger scale, but it could be a better idea to use Threads which don't loop infinitely and suck up processing power.

Comments

1

Yes, it is possible. Just make sure, there is a different thread that can handle some kind of input...

public class MyBreakableInfiniteLoop

    public static void main(String[] args) {

        MyRunnable r = new MyRunnable();
        new Thread(r).start();

        System.out.println("Press Enter to stop thread");
        new Scanner(System.in).readLine();
        r.stop = true;

    }

    public static class MyRunnable extends Runnable {
       public volatile boolean stop = false;
       public void run() { 
           while(!stop) { 
              //do nothing, preferably in a meaningful way 
           } 
       }
    }


}

(aLso, I didn't take into count kill -9 as "input that breaks the infinite loop"...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.