0

I'm trying to build a program which reads a certain String after using the Split function

import java.util.Scanner;

   public class Lexa2 {

public void doit() {
    String str = "( 5 + 4 ) * 2";
    String [] temp = null;
    temp = str.split(" "); 
    dump(temp);
}
public void dump(String []s) {
    for (int i = 0 ; i < s.length ; i++) {           
        if (s[i] == "(") {              
            System.out.println("This is the left paren");
        } else if (s[i] == ")"){                
            System.out.println("This is the right paren");          
        }else  if (s[i] == "+"){                
            System.out.println("This is the add");          
        }else  if (s[i] == "-"){                
            System.out.println("This is the sub");          
        }else  if (s[i] == "*"){                
            System.out.println("This is the mult");         
        }else  if (s[i] == "/"){                
            System.out.println("This is the div");          
        }else               
            System.out.println("This is a number");
    }
}   

     public static void main(String args[]) throws Exception{
       Lexa2 ss = new Lexa2();
         ss.doit();
 }
    }

The output should be something like this:

This is the left paren
this is a number
this is the add
this is the right paren
this is a number
5
  • So, what is the question? Your code looks reasonable. Commented May 11, 2012 at 10:13
  • 10
    DON'T USE == FOR STRING COMPARISON I say it loudly because it has been repated so many times in a softer voice, maybe you need it. Commented May 11, 2012 at 10:13
  • 1
    15.21 Equality Operators Commented May 11, 2012 at 10:16
  • 1
    possible duplicate of Java String.equals versus == Commented May 11, 2012 at 10:21
  • Take a reference to a good Java book(s). Commented May 11, 2012 at 10:24

2 Answers 2

4

you're pretty close, just replace (s[i] == "?") with (s[i].equals("?"))

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks i always mess up with the lamest mistakes :)
1

Don't use s[i] == ")" to compare strings. In this way, you're not checking if the string in s[i] is equal to ).

Use the equals method. Then you can compare the strings using:

if (s[i].equals("("))

Replace equals in the other if statements.

UPDATE

P.S. I think the best way to compare strings, looking to your code, is to use the switch/case statements. But, this feature is only available in Java 7. I think this will avoid the continuous checking with if statements, and the code will be more readable. If you have Java 7, use this feature, otherwise, in case of Java 6 or lower, follow @pstanton suggestion. ;-)

2 Comments

switches don't work with strings, so he/she would need to use char
@pstanton You're right, indeed this feature is available only in Java 7. Thank you :-) Answer updated! For further info, take a look to: stackoverflow.com/questions/338206/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.