strings are not equal

Posted on 2006-05-19
Last Modified: 2010-03-31

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

import java.util.ArrayList;

public class Translation {
      String rawData = "abcdefgeryuidfwqeudweufydiyfvwefiwsdbicweiyubfd";
      String[] uniqueElements = new String[4];
      public Translation(){
      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");

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?


Question by:waffe
    LVL 14

    Accepted Solution

    compare strings using
    not by
    == // it just compares the object's references not their values
    LVL 14

    Expert Comment


    Author Comment

    Ahh yes, I kind of remember reading about this - it all makes sence now! - thanks hoomanv!



    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    FizzBuzz challenge 9 65
    nested class vs inner class 5 36
    scoresAverage challenge 8 58
    array6 challenfge 6 48
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now