-1

I am iterating through my hashMap object to fill up my array with either 0 or 1; I have a condition that when a record in my hashMap object equals "positive" then write "1" to my array else write "zero" to my array.

When I print the content of my array; all cells contain zeros, which is not the case.

Can you please advise why???

int index = 0;
        if ( (!map.isEmpty()) && (index < PTFindings.length ) ) {
            Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();

                if(entry.getValue().equals("Positive")) {
                    PTFindings[index] = 1;
                    index++;
                }

                else if (entry.getValue().equals("Negative") ) {
                    PTFindings[index] = 0;
                    index++;
                }

                Log.d("map values", entry.getKey() + ": " +
                        entry.getValue().toString());

                System.out.println("PTFindings at index " + index + " : " + PTFindings[index]);
            }
        }
Mike
  • 91
  • 2
  • 10
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – azurefrog Nov 28 '15 at 21:46
  • `entry.getValue() == "Positive"` will not evaluate to what you think it will, so you won't actually be setting any of the values to `1`, or incrementing `index`. – azurefrog Nov 28 '15 at 21:47

1 Answers1

0

You are comparing strings with two equals sign (==), don't do that. Use strings .equals() method e.g

if(entry.getValue().equals("Positive"))

You can find more on this here How do I compare strings in Java?

Community
  • 1
  • 1
hocikto
  • 871
  • 1
  • 14
  • 29
  • Comparison works just fine; my issue is how to increment the index of my array? – Mike Nov 28 '15 at 21:44
  • The index of my array (PTFindings) never get increment; it is always at 0 (index++ not doing anything) – Mike Nov 28 '15 at 21:46
  • 1
    @Mike because both of your conditions will always evaluate to false. Since you never enter your if or else-if block, it's never incremented. – azurefrog Nov 28 '15 at 21:47
  • @Mike Well you are doing index++ inside "if", and "else if", and it is never incremented, so maybe comparison does not work and you never get into "if" or "else if" block of code? Try to increment index out of those blocks and you will see it works. Just compare strings with equals method – hocikto Nov 28 '15 at 21:48
  • Thank you, I modified my code but now I am getting an out of bound exception !!!! – Mike Nov 28 '15 at 22:01
  • @Mike If this answer solved the problem in your initial question (index not incrementing), then you should accept it and ask a separate question about the out-of-bounds exception, posting your updated code there. – azurefrog Nov 28 '15 at 22:09