This site requires JavaScript, please enable it in your browser!
Greenfoot back
erdelf
erdelf wrote ...

2012/10/17

problem with unexpected return

1
2
3
erdelf erdelf

2012/10/18

#
I thank you for your help. I should have thought of the additem method earlier
erdelf erdelf

2012/10/18

#
ok, another problem in this code:
        for(int i = 1;  i < items.length; i++)
        {
            String item = items[i];
            System.out.println(items[i] + " " + items[3]);
        }
it seems that the for-circle is only executed one time. I tested with the printing of the third item and it printed it without problems, I dont know why this seems to be executed just one time.
danpost danpost

2012/10/18

#
How many lines does the for loop print out in the terminal (when executed one time)?
erdelf erdelf

2012/10/19

#
one line, with the first item and the third
danpost danpost

2012/10/19

#
Is there a reason YOU are skipping the first item in the 'items' array? Remember, 'items' is the first element in the array. 'items' is the FOURTH element in the array. The 'for' loop, since you are starting 'i = 1' (instead of 'i = 0') will execute through itself one less than the number of elements in the 'items' array.
erdelf erdelf

2012/10/19

#
yeah sure, I should say this, the first element is not an item, it is another value and in this for loop I dont want the first element. I think I just discovered the problem, after 18 hours. this line:
for(int i = 1;  i < items.length; i++)
is wrong I think, I think this is the right line
for(int i = 1;  i > items.length; i++)
SPower SPower

2012/10/19

#
Would only work when items.length is 0, and then loop forever.
erdelf erdelf

2012/10/19

#
sure? I didn't used the for method a while, but I thought that my for-loop would only stop if the length of the array is reached
danpost danpost

2012/10/19

#
The correct 'for' loop line is
for (int i = 1; i < items.length; i++)
If you are only calling 'addItem' twice after initializing the 'items' array, then it should only execute one time.
SPower SPower

2012/10/19

#
< means smaller than, > means bigger than (as a reminder). and about for loops, it will only execute what's between the curly braces {} when this (for example):
i > items.length;
is true. So this:
for(int i = 1;  i > items.length; i++)
would execute if items.length is smaller then i. i starts off as 1, so it can only begin when items.length is 0. When that happens, it will go on until you die with an MemoryOutOfBoundsException or you can't store the number in i anymore (since an integer doesn't have infinite storage, just 4 bytes / 32 bit).
erdelf erdelf

2012/10/19

#
thx SPower for the reminder. well, the first element is automatically set, I used the additem three times, so it should have four elements, and three items so it should be executed three times
danpost danpost

2012/10/19

#
OK, put this line before the loop:
System.out.println("Number of elements: " + items.length);
and see what it says (report back).
erdelf erdelf

2012/10/19

#
Number of Elements: 4. so it is right.
danpost danpost

2012/10/19

#
Then the loop will execute three times. Alright, change the 'for' loop to the following and report back
for(int i = 1;  i < items.length; i++)
{
    String item = items[i];
    System.out.println("'" + item + "'");
}
erdelf erdelf

2012/10/19

#
only one time executed
There are more replies on the next page.
1
2
3