-1

I'm currently using a for loop to print elements in a list. It works fine and prints all the lines, however, nothing prints after the for loop itself.

For example:

for(int i=0;i<=petlist.size();i++)
{
    System.out.println(petlist.get(i));
}

System.out.println("Test");

Test won't print. Is there any other way to do this? Or am I just missing something?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

5 Answers5

1

The loop should continue untill i < petlist.size(). In your code it will be an ArrayIndexOutOfBoundsException.

for(int i=0; i < petlist.size();i++) {}
Andrew Gies
  • 719
  • 8
  • 20
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • thanks @user3170143. welcome to the so :). if you think any of the answers was helpful you can accept those by clicking the check mark. – stinepike Jan 08 '14 at 05:03
0

Your loop should like

for(int i=0;i<petlist.size();i++)
{
    System.out.println(petlist.get(i));
}

System.out.println("Test");

I have removed =< to <

Oomph Fortuity
  • 5,710
  • 10
  • 44
  • 89
0

Try this:

for(int i=0;i<petlist.size();i++)
{
    try{
       System.out.println(petlist.get(i));
    }
    catch(Exception ex){
       ex.println();
    }
}

I believe this should catch your error if you have it!

But I believe your loop never ends so it never gets out of it, I believe its the <= needs to be <

I am Cavic
  • 1,115
  • 1
  • 10
  • 22
0

Instead of i<=petlist.size() use i<petlist.size() otherwise it will give IndexOutOfBound Exception.

Sinto
  • 920
  • 5
  • 10
0
for(int i=0;i<petlist.size();i++)
{
    System.out.println(petlist.get(i));
}

System.out.println("Test");
xrcwrn
  • 5,339
  • 17
  • 68
  • 129