dimanche 15 juillet 2018

java string wont equal in if after split()

there is online game where NPCs give you list of goodies they want to buy. And there was a great program that let you just copy the list and it would calculate if you profit or not (no longer avaliable anywhere).

111 x Mrkev
120 x Salát
Částka: 8,04 Zt

This is example list copied form one of the NPCs.

I paste this to a JtextField and than with button I get string that goes through this code:

    //Get the text from the field
    order = textPane.getText();

            //This makes it one line with ; in between each "word". Or should rather
            order = order.replaceAll("\\r?\\n", ";");
            order = order.replaceAll(" ", ";");



            System.out.println(order);

            //This is what it prints out (so far so good):
            //111;x;Mrkev;120;x;Salát;Částka:;8,04;Zt

            //This should split the string in individual pieces
            String arr[] = order.split(";");

            //how many pieces are in array
            int size = arr.length;

            //System.out.println(size);


            //Now, for the first step I only need "Mrkev" and "Salát" so I jump from 2 to 5, ... If the list was longer it would be every third thing.
            for(int i = 2; i < size; i= i+3) {

                //prints out correct things, without any visible damage
                System.out.println(arr[i]);

                //"Test code"
                String item = arr[i];
                System.out.println(item);


                //This doesnt work
                if(test == "Mrkev") {
                    System.out.println("OK");
                }

            }

I thing it should work. I have done everything correct (I think). It should be equal. Any combination of if(arr[i]/arr[2]... == "Mrkev")... wont work.

But if I compare for the number 111 like this:

    if(Integer.parseInt(arr[0]) == 111) {...}

it works for some reason.

Aucun commentaire:

Enregistrer un commentaire