blerch
blerch

Reputation: 395

While Loop Issue: Does not pause

I made some kind of mistake in this loop, and I really can't figure it out. Here's the loop:

while (true) {
        System.out.print(stepName[currentTick]);

        for(int i = stepName[currentTick].length() - longestNameInt; i == 0; i--)
            System.out.print(" ");

        System.out.print(" [");

        double percentCalc = (double) stepPercent[currentTick];
        int slotsRep = (int) Math.round((percentCalc * 0.2));

        for(int i = slotsRep; i == 0; i--)
            System.out.print("*");

        for(int i = 20 - slotsRep; i == 0; i--)
            System.out.print(" ");

        System.out.print("] " + stepPercent[currentTick] + "% \r");

        if(currentTick == totalTicks)
            break;

        Thread.sleep(stepTime[currentTick]);
    }

Basically, it just keeps printing '(First stepname) [*] 0%' rapidly. I'm sorry if it's super obvious, but I'm kinda a noob. :)

P.S. Please ask me if you need more of my class.

Upvotes: 0

Views: 78

Answers (3)

Prateek
Prateek

Reputation: 12242

Please change this :

for(int i = stepName[currentTick].length() - longestNameInt; i == 0; i--)

TO

for(int i = stepName[currentTick].length() - longestNameInt; i >= 0; i--)

only comparison mistake is there.

Also make it sure that value of current tick or totalticks changes for the loop to break, as may be it can happen that they will never achive the equality state and your loop goes infinite because of while(true) condition.

Upvotes: 4

Shamse Alam
Shamse Alam

Reputation: 1315

Make following corrections

  1. for(int i = stepName[currentTick].length() - longestNameInt; i >= 0; i--)

     2.    `for(int i = slotsRep; i >= 0; i--)
    
      3.  `for(int i = 20 - slotsRep; i == 0; i--)`
    

Upvotes: 1

Rahul
Rahul

Reputation: 45060

You have a infinite while loop here.

if(currentTick == totalTicks)
    break;

You don't seem to be changing the values of either currentTick or totalTicks at all but the break from the while loop is supposed to happen when both the above 2 values are equal. Since they are not equal initially and you never change them in the while, it becomes an infinite loop.

Upvotes: 0

Related Questions