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

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;
}

Hi.

Thanks for the answer :)

Looks really nice and useful..

Great and thanks a lot for the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.