0

Hi.

I´m having some trouble with my search code.

I'm trying to make it search in either my varenummer field or varenavn field.

My method to do so looks like this:

public KasseInterface søgVare( String varenummer, String varenavn ) {
            System.out.println( "Søger efter vare med varenummer: " + varenummer);
            for( KasseInterface elem : varer ) {
            if (elem.getVarenummer().equals(varenummer) ) {                
            return elem;
            }
            else if (elem.getVarenavn().equals(varenavn) ) { 
                System.out.println("fandt vare '" + elem + "'");            
                return elem;
                }
            }        
            System.out.println("fandt ikke nogen vare!!!");
            return null;
        }

Somehow it'll only search for my varenavn, not my varenummer, can someone see why?

Also.. I would like to make it so you can either enter varenummer or varenavn, but I haven't figured how to do this. I have tried to do this :

public KasseInterface søgVare( String varenummer || String varenavn ) {

without any luck.

Can anyone help?

Thanks.

Regards

Christian

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by chr.kj
0

If you just want to return an element if a certain value matches either of two fields, all you really need to do is this

public KasseInterface søgVare(String value) {
    for( KasseInterface elem : varer ) {
        if (elem.getVarenummer().equals(value) || elem.getVarenavn().equals(value)) {
            return elem;
        } 
    }
    System.out.println("fandt ikke nogen vare!!!");
    return null;
}
0

Hi.

Thanks for the answer :)

Looks really nice and useful..

Great and thanks a lot for the help :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.