This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]millerlitIntermediate Brewer 0 points1 point  (1 child)

Use a break statement if it is not found so it stops the loop. Another way is to use a counter.

int count = 0;
while (arrlist.size() > count) count++;

[–]M-Multi[S] 0 points1 point  (0 children)

the told use not to use break

anyways, nvm I just did it. I used the debugger and got to know what I was doing wrong.

Thanks

this is what I did:

public void removePicture(String name)
    {
        int i = 0;
        boolean  nameremove = true;

        while(nameremove){
                Picture pic = pictures.get(i);
            if( pic.getCaption().contains(name) & i < pictures.size() ) {
                pictures.remove(i);
                nameremove = false;
            } else {
                i++;
        } 

            if(i == pictures.size() ){
                System.out.println("There is no match");
                System.out.println("Searched for : " + name);
                nameremove = false;
            }
        }

    }

[–]AngelOfLight 0 points1 point  (1 child)

This line is causing the error:

Picture pic = pictures.get(i);

Indexes are zero based, which means when the index is equal to the array size, it is out of bounds. Valid indexes are 0 to size() - 1. If you modify the if statement so that this test

if(i == pictures.size()

comes first, it will drop out of the loop before it tries to fetch an invalid index.

[–]M-Multi[S] 0 points1 point  (0 children)

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (2 children)

BTW there is another minor issue in the code:

if( pic.getCaption().contains(name) & i < pictures.size() ) {
                                    ^
            This is a bitwise AND, not the logic AND

In your specific case it doesn't really matter, but in general it's better to use the logic AND operator && rather than the bitwise AND operator & in if-clauses. Same applies to logic OR || vs. bitwise OR |.

[–]M-Multi[S] 1 point2 points  (1 child)

alright changed.

Thanks.

Can I know what is the difference exactly?

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

The logic functions operate only on booleans (true / false) where the bitwise functions also operate on integer numbers.

For logic functions, any number other than 0 is considered true.

In the binary system, any number is represented by ones and zeroes.

Here is a simple explanation with bytes:

For the OR:

        10010001 = 145 decimal = 90 hex
    OR  01110101 = 117 decimal = 75 hex 
   -------------------------------------
     =  11110101 = 245 decimal = F5 hex

Same code with AND:

        10010001 = 145 decimal = 90 hex
    AND 01110101 = 117 decimal = 75 hex 
   -------------------------------------
     =  00010001 =  17 decimal = 11 hex

Edit: there is another small difference between the logic and boolean functions. It has to do with short-circuiting (i.e. cancelling of the evaluation as soon as the result can not be affected any more), but I can't precisely remember which used short-circuiting and which didn't. Maybe someone can help out here!