0

I'm working on a Java program that takes an input String called input which looks like

'(A A A A B C C D E E E E)

And my output, A String[] called Letters that should look like

[A,B,C,D,A,E]

And another Array, an int[] called count that should look like

[4,1,2,1,2,4]

And I have code that looks like below, but for some reason, every time I run it it won't work. The print statements read that it doesn't ever go to the if statement. Line 2 on the second loop reads:

Check if A == A

Then it goes to the else statement and prints

A != A

What am I doing wrong? Why is A (items[0]) not equal to A (checkagainst)?

 private static boolean func2(String input){ /*input: '(A A A A B C C D E E E E)
    input = input.replace("'","");//replace to null char
    input = input.replace("(","");//replace to null char
    input = input.replace(")","");//replace to null char
    System.out.println(input);
    String[] items = input.split(" |\u0000",0);
    System.out.println("Array items "+Arrays.toString(items));

    int position = 0;
    int countlength = 0; for (String z: items)countlength += 1;//determines the MAX length of the letters array
    String checkagainst = "";
    String[] letters = new String[countlength];
    int[] count = new int[countlength];
    System.out.println("\n Letters: "+Arrays.toString(letters)
            +"\nCount: "+Arrays.toString(count)
            +"\nItems: "+Arrays.toString(items));
    for (int x = 0; x < items.length; x++){
        System.out.println("\n\nCheck if"+checkagainst+"=="+items[x]);
        if (checkagainst == items[x]){
            count[position] += 1;
            System.out.println("Iteration:" + x
                    +"\n Letters: "+Arrays.toString(letters)
                    +"\n Count: "+Arrays.toString(count)
                    +"\nSwitch: if");
        }
        else{
            System.out.println("checkagainst != items[x]");
            letters[position] = items[x];
            count[position] = 1;
            checkagainst = items[x];
            position ++;
            System.out.println("Iteration:" + x
                                +"\n Letters: "+Arrays.toString(letters)
                                +"\n Count Arrays"+Arrays.toString(count)
                                +"\ncheckagainst: "+ checkagainst
                                +"\nSwitch: else");
        }
    }
  • 3
    What is `checkagainst`? – Mike G Feb 06 '15 at 04:05
  • 3
    You're not providing enough information to definitely allow us to understand your problem and thus assuredly answer it, but I'm guessing that you're mistaking object equality, `==` for functional equality. The Arrays class will likely have a method you will want to use, `equals(...)` or `deepEquals(...)`. – Hovercraft Full Of Eels Feb 06 '15 at 04:07
  • what's the declaration of those array, and how you are populating the data? – Adrian Shum Feb 06 '15 at 04:07
  • 1
    You mention String[] arrays and int[] arrays which we cant see in your code. Are you trying to compare Strings and ints using == (because that's not going to work) – slipperyseal Feb 06 '15 at 04:08
  • As far as I can tell you are trying to compare strings with == the == operator checks equality by reference (location in memory) and not by what the string is you should use '(thisString.equals(otherString))' – J Blaz Feb 06 '15 at 04:12
  • 1
    Thank you, @David Wallace, the post you linked me fixed my problem. Sorry for posting a dupe. – metal.slime.angel Feb 06 '15 at 04:16
  • 1
    Don't apologize for posting the dup so much as for posting an incomplete question. Always please consider your question from our point of view, folks who have no idea what your problem is, and then strive to hone down onto and clarify your problem for us so it is easy to answer. – Hovercraft Full Of Eels Feb 06 '15 at 04:18
  • Also @metal.slime.angel there's nothing wrong with having a question closed as a dupe. It just acts as another signpost. – Mike G Feb 06 '15 at 04:20

0 Answers0