24

I am trying to make java go trough a list of numbers. It chooses the first one, gives this as output, waits/sleeps like 2000 milliseconds and then give the next one as output, waits 2000 milliseconds, etc.

They should not be listed behind eachother so when my list is: 10 20 30 40 50

It should not give as output: 10 20 30 40 50. But just 50.

It would be even better if it was able to repeat itself.

So far i tried:

List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
Iterator<Integer> myListIterator = someList.iterator(); 
while (myListIterator.hasNext()) {
    Integer coord = myListIterator.next();     
}

But this has no timer, and i am not sure if this will only show '20' or '10 20 30 40 50' as output. And i dont really know how to put a sleep/wait command and a repeat command in this :s (might have overlooked the repeat command if its already in)

Edit Tyvm all, i can go on with the rest of the coding now :)

4
  • 3
    This program doesn’t have any output. Commented Mar 20, 2011 at 21:44
  • 1
    In addition, you contradict yourself. First you say It chooses the first one, gives this as output, ... and then give the next one as output then later It should not give as output: 10 20 30 40 50. But just 50. Commented Mar 20, 2011 at 21:47
  • 5
    @Brian Roach he means it rewinds and rewrites the line. (\r) Commented Mar 20, 2011 at 21:51
  • 1
    @mathepic - Impressive, I could not have pulled that from the question without you pointing it out. Commented Mar 20, 2011 at 21:57

5 Answers 5

27

If you want to rewrite a line on console, print a control character \r (carriage return).

List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
Iterator<Integer> myListIterator = myCoords.iterator(); 
while (myListIterator.hasNext()) {
    Integer coord = myListIterator.next();     
    System.out.print("\r");
    System.out.print(coord);
    Thread.sleep(2000);
}
Sign up to request clarification or add additional context in comments.

2 Comments

this might be late but,i just for started with list. Whats the difference between using a for loop like; for(i<list.size();i++) and a while loop with hasNext()?
@alexsummers It is just a different method of accessing collection elements. See stackoverflow.com/questions/99164/… and stackoverflow.com/questions/89891/…
2

code that works, but output is:

10
20
30
40
50

so:

    List<Integer> myCoords = new ArrayList<Integer>();
    myCoords.add(10);
    myCoords.add(20);
    myCoords.add(30);
    myCoords.add(40);
    myCoords.add(50);
    for (Integer number : myCoords) {
        System.out.println(number);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

Comments

1

To insert a sleep command you can use Thread.sleep(2000). So the code would be:

List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
Iterator<Integer> myListIterator = someList.iterator(); 
while (myListIterator.hasNext()) {
    Integer coord = myListIterator.next();    
    System.out.println(coord);
    Thread.Sleep(2000);
}

This would output: 10 20 30 40 50

If you want the numbers after each other you could use: System.out.print(coord +" " ); and if you want to repeat the section you can put it in another while loop.

List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
while(true)
    Iterator<Integer> myListIterator = someList.iterator(); 
    while (myListIterator.hasNext()) {
        Integer coord = myListIterator.next();    
        System.out.print(coord + " ");
        Thread.Sleep(2000);
    }
}

This would output: 10 20 30 40 50 10 20 30 40 50 ... and never stop until you kill the program.

Edit: You do have to put the sleep command in a try catch block

5 Comments

So what i change in that piece of code to make it just rewrite the previous number? something with the System.out.print(coord + " "); ? And 'try catch block' ? That string looks ok for me in the way u just wrote it :P
AH, so like: try{ Thread.sleep(2000); }catch(Exception e){ // handle the exception... }; instead of Thread.Sleep(2000);
About the rewrite, or did u mean it is rewriting its previous output?
'try and catch block' is relevant here because the method sleep may throw an exception. You can learn about it more here: download.oracle.com/javase/tutorial/essential/exceptions
Ah so it would become: (see my 2nd post in this question)
1

Let's use some java 8 feature:

IntStream.iterate(10, x -> x + 10).limit(5)
  .forEach(System.out::println);

If you need to store the numbers you can collect them into a collection eg:

List numbers = IntStream.iterate(10, x -> x + 10).limit(5)
  .boxed()
  .collect(Collectors.toList());

And some delay added:

IntStream.iterate(10, x -> x + 10).limit(5)
  .forEach(x -> {
    System.out.println(x);
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      // Do something with the exception
    }  
  });

Comments

0

So it would become:

List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
while(true)
    Iterator<Integer> myListIterator = myCoords.iterator(); 
    while (myListIterator.hasNext()) {
        Integer coord = myListIterator.next();    
        System.out.print("\r" + coord);
        try{
    Thread.sleep(2000);
  }catch(Exception e){
   // handle the exception...
  }
    }
}

5 Comments

To remove previous outcome: System.out.print("\r" + coord);
:O, hmm.. how else can i make it rewrite the previous outcome u think?
with System.out.print(coord + " "); instead of the System.out.print("\r" + coord); perhaps?
and not someList. but your list - means -> ` Iterator<Integer> myListIterator = myCoords.iterator(); `
What operating system are you using and where are you printing to? "\r" works on Linux/Mac terminal (tested) and should work in Windows console. But if you are looking at the output in some IDE (NetBeans, Eclipse) then it might not work.

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.