0

Possible Duplicate:
How do I compare strings in Java?

I have an array of Strings, the array is called Morse:

private final static String[] Morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
    "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",  ".-.", "...", "-", "..-",
    "...-" ,".--" ,"-..-", "-.--", "--..",".-.-.-","--..--","..--.." ,"|"};

private final static String[] Colours = {"red","green","white"};

Then there is my code:

char[] stringArray;
stringArray = converttoMorse(letter).toCharArray();

char ch;

for(int i=0;i<stringArray.length;++i)
{
    if(stringArray[i]== "." ) //Problem here
    {
        System.out.println(Colours[0]);
    }
    else if (stringArray[i] == "-" ) //Problem here
    {
        System.out.println(Colours[1]);
    }
    else
    {
        System.out.println(Colours[2]);
    }
}

This is only a snippet of my actual program, the english to morse is working absolutely perfect. HOWEVER, I would like to indicate the dots and dashes as colors "red" and "green" respectively as string (with spaces indicating white).

I used the toCharArray() method to breakdown the printed outcome Morse as an array. However I'm not able to correspond each of these dots and dashes to their specific colours. For example, I would like ".-" to show as "red green". How can I do this? Is there a specific method for doing so?

Community
  • 1
  • 1
  • use seperate if's instead of if,else if,else. write as if(cond){print red};if(cond){print green}; if(cond){print white}, then u will get ur desired output. – tausun Jan 29 '13 at 11:15

3 Answers3

4

Because stringArray is a char[] you need to compare it's contents with a char.

You need:

if (stringArray[i] == '.') 
// '.' is the dot character,
// "." is a String that contains only the dot character 

If you want to compare Strings, you should read this question: How do I compare strings in Java?

Community
  • 1
  • 1
jlordo
  • 37,490
  • 6
  • 58
  • 83
  • How silly of me not to realize this mistake! Thank you very much! The program is up and running! I'll keep this silly char, string quotation differences in my head next time ;) ! – Omar Alfred Jan 28 '13 at 00:58
  • @OmarAlfred: if this was a helpful answer and solved your problem, you should accept it for others to see that the problem is solve. See: [How does accepting an answer work?](http://meta.stackexchange.com/a/5235/186652) – jlordo Jan 28 '13 at 07:00
0

The way I see it, you've got a String[], and you want the result to be a String[] to indicate red-green. To do that, we're going to use a bit of regular expressions and an outside method. I wouldn't even want to mess with the char array, since going from that to the String array would be less straightforward.

Note: This presumes that no elements in your array are null, or this will definitely blow up. You can do a simple null check before the replaceAll to mitigate this.

public static String[] convertToRedGreen() {
    // Since the array we want is static, our method is too.
    String[] redGreen = new String[Morse.length];
    for(int i = 0; i < Morse.length; i++) {
        redGreen[i] = Morse[i].replaceAll("[.]", " red ").replaceAll("[-]", " green ");
    }
    return redGreen;
}

This will give you a String[] which looks something like this.

[ red  green ,  green  red  red  red ,  green  red  green  red ,  green  red  red ,  red ,  red  red  green  red ,  green  green  red ,  red  red  red  red ,  red  red ,  red  green  green  green ,  green  red  green ,  red  green  red  red ,  green  green ,  green  red ,  green  green  green ,  red  green  green  red ,  green  green  red  green ,  red  green  red ,  red  red  red ,  green ,  red  red  green ,  red  red  red  green ,  red  green  green ,  green  red  red  green ,  green  red  green  green ,  green  green  red  red ,  red  green  red  green  red  green ,  green  green  red  red  green  green ,  red  red  green  green  red  red , |]
Makoto
  • 104,088
  • 27
  • 192
  • 230
-1

Don't use == to compare strings: see here

Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
  • `stringArray[i]` is a `char`, so it's comparing primitives together. No problems with using `==` for that. – Makoto Jan 27 '13 at 21:50