Phil
Phil

Reputation: 179

Alternative to substring

I have a strange problem when adding a value to a String array which is later involved in an array sort using a hash map. I have a filename XFR900a, and the XFR900 part is added to the array using the following code;

private ArrayList<String> Types = new ArrayList<String>();

...

Types.add(name.substring(0,(name.length() - 1));
System.out.println(name.substring(0,(name.length() - 1));

I even print the line which gives "XFR900", however the array sort later on behaves differently when I use the following code instead;

Types.add("XFR900");
System.out.println(name.substring(0,(name.length() - 1));

which is simply the substring part done manually, very confusing.

Are there any good alternatives to substring, as there must be some odd non ascii character in there?

Phil

UPDATE

Thanks for your comments everyone. Here is some of the code that later compares the string;

    for (int i=0;i< matchedArray.size();i++){

        //run through the arrays
        if (last == matchedArray.get(i)) {
            //add arrays to a data array
            ArrayList data = new ArrayList();
            data.add(matchedArray1.get(i));
            data.add(matchedArray2.get(i));
            data.add(matchedArray3.get(i));
            data.add(matchedArray4.get(i));
            data.add(matchedArray5.get(i));
            //put into hash map 
            map.put(matchedArray.get(i), data);
        }
        else {
            //TODO
            System.out.println("DO NOT MATCH :" + last + "-" + matchedArray.get(i));

As you can see I have added a test System.out.println("DO NOT MATCH" ... and below is some the output;

DO NOT MATCH :FR99-XFR900 DO NOT MATCH :XFR900-XFR900

I only run the substring on the XFR900a filename. The problem is that for the test line to be printed last != matchedArray.get(i) however they are then the same when printed out to the display.

Phil

Upvotes: 4

Views: 2595

Answers (3)

Ankush soni
Ankush soni

Reputation: 1459

Never use == operator if you wanted to check the value since operator will check the Object reference equality, use equals operator which check on the value not the reference i.e.

for (int i=0;i< matchedArray.size();i++){

//run through the arrays if (last.equals(matchedArray.get(i))) { // Line edited //add arrays to a data array ArrayList data = new ArrayList(); data.add(matchedArray1.get(i)); data.add(matchedArray2.get(i)); data.add(matchedArray3.get(i)); data.add(matchedArray4.get(i)); data.add(matchedArray5.get(i)); //put into hash map map.put(matchedArray.get(i), data); } else { //TODO System.out.println("DO NOT MATCH :" + last + "-" + matchedArray.get(i));

Upvotes: 0

Phil
Phil

Reputation: 179

Use .equals() rather than == because they are strings!

if (last.equals(matchedArray.get(i))) {}

Upvotes: 0

Andr&#233; Stannek
Andr&#233; Stannek

Reputation: 7863

You should never use the == operator to compare the content of strings. == checks if it is the same object. Write last.equals(matchedArray.get(i)) instead. The equals() method checks if to object are equal, not if they are the same. In case of String it checks if the two strings consists of the same characters. This might eliminate your strange behaviour.

PS: The behaviour of == on string is a little unpredictable because the java virtual machine does some optimization. If two strings are equal it is possible that the jvm uses the same object for both. This is possible because String objects are immutable anyway. This would explain the difference in behaviour if you write down the substring manually. In the one case the jvm optimizes, in the other it doesn't.

Upvotes: 3

Related Questions