Home > DeveloperSection > Forums > Infinite loop in java
Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    November-13-2014 12:34 AM

 Java Netbeans  For Loop  Loop 
Ratings:


 1 Reply(s)
 736  View(s)
Rate this:

I am trying to solve a problem using java where I have to print the number the same number of time as the number itself. For example 1 will be printed once 2 twice 3 thrice and so on. I tried to attempt the question using nested loop but it is going into infinite loop. Please pinpoint the mistake in the code. Thanks!

 {                                        

    for (int i=1;i<=10;i=i+1) {

        for (int j=1;j<=i; j=i) {

            jTextArea1.append(""+j);

        }

    }

 }         

P.S I attempted this question using netbeans.



Alex Leblois
Alex Leblois

Total Post:67

Points:471
Posted on    November-13-2014 3:41 AM

The problem is in the second loop.

Making progress as j=i and checking for j<=i will always give the true result. So there is the infinite loop.

You may want to change the progress to something like j= j+1

Edit: You need to do this

for (int i=1;i<=10;i=i+1) {

    for (int j=1;j<=i; j++) {

         jTextArea1.append(""+i);

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick