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.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Joyal
  • 13
  • 1
  • 3

3 Answers3

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.

Community
  • 1
  • 1
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • 2
    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. – nhgrif Oct 01 '13 at 19:50
  • If I have average = total / 10 How do I do average for infinite loop? – Joyal Oct 01 '13 at 19:51
  • 1
    If you need to average, the `for` loop is actually pretty good method. `for(int i=0;;i++){//find sum, divide by i}` – nhgrif Oct 01 '13 at 19:52
  • 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++ – DotNetRussell Oct 01 '13 at 19:53
  • @AMR i have, in the end to take the average of all data entered. How do I do that if I changed it from set 10 to infinite? – Joyal Oct 01 '13 at 19:54
  • @Cube I hate to state the obvious but if your loop is infinite it never ends. How do you get the average of something that never ends? – DotNetRussell Oct 01 '13 at 19:55
  • @AMR When I do use a command like end can I take the average of where it ended? – Joyal Oct 01 '13 at 19:57
  • @Cube Also please tell me what you are attempting to average? – DotNetRussell Oct 01 '13 at 19:57
  • @AMR Yea, I have to make a Gradebook data entry thing for homework and in the end it should take average of all the data entered. So If I enter 3 in the end it should give me the average of the 3 if I enter 500 then the average of 500. Thanks for your time – Joyal Oct 01 '13 at 20:00
  • @Cube that should be your solution above – DotNetRussell Oct 01 '13 at 20:04
  • 1
    @SamYonnou That concept is probably a bit to advanced for someone who would even post this original question... – nhgrif Oct 01 '13 at 20:08
  • @AMR Thank you so much for your help. If it doesn't work can I reply back? – Joyal Oct 01 '13 at 20:12
  • @Cube I have given you the tools and the resources to do this project. There is a time when you need to do some research yourself. This is a pretty basic CIS problem. If you want to succeed in future classes you need to learn to get friendly with Java Documentation. Good luck bud – DotNetRussell Oct 01 '13 at 20:13
  • @AMR Thanks, sorry I just wasn't sure how to average an infninite loop – Joyal Oct 01 '13 at 20:27
  • @Cube if this answers your question please make sure to select the check mark so future people know which answer works – DotNetRussell Oct 01 '13 at 21:47
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.

Michael Hoyle
  • 249
  • 2
  • 9
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"...)

ppeterka
  • 20,583
  • 6
  • 63
  • 78