?
Solved

strings are not equal

Posted on 2006-05-19
3
Medium Priority
?
246 Views
Last Modified: 2010-03-31
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
Comment
Question by:waffe
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 400 total points
ID: 16723269
compare strings using
s1.equal(s2)
not by
== // it just compares the object's references not their values
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16723271
s1.equals(s2)
0
 

Author Comment

by:waffe
ID: 16723319
Ahh yes, I kind of remember reading about this - it all makes sence now! - thanks hoomanv!

waffe

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question