• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

strings are not equal

Hi,

I am having some problems with a conditional statement and two strings.

[code]
import java.util.ArrayList;

public class Translation {
      String rawData = "abcdefgeryuidfwqeudweufydiyfvwefiwsdbicweiyubfd";
      String[] uniqueElements = new String[4];
      public Translation(){
            intUE();
            convetData();
      }
      public void intUE(){  
            uniqueElements[0] = "a";
            uniqueElements[1] = "c";
            uniqueElements[2] = "e";
            uniqueElements[3] = "g";      
      }
      public void convetData(){
            int counter = 0;
            for(int i=0; i<rawData.length(); i++){
                  for(int j=0; j<uniqueElements.length; j++){
                        if(uniqueElements[j] == rawData.substring(counter, counter+1)){
                              System.out.print("data"+rawData.substring(counter, counter+1) +"\n");
                        }
                  }
            }
            counter++;
      }
}
[\code]


The if condition in convetData() never becomes true but the two strings are equal as they are when I trace them individually. What am I missing?

Thank,

waffe
0
waffe
Asked:
waffe
  • 2
1 Solution
 
hoomanvCommented:
compare strings using
s1.equal(s2)
not by
== // it just compares the object's references not their values
0
 
hoomanvCommented:
s1.equals(s2)
0
 
waffeAuthor Commented:
Ahh yes, I kind of remember reading about this - it all makes sence now! - thanks hoomanv!

waffe

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now