User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,523 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,591 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2539 | Replies: 1
Reply
Join Date: Aug 2007
Posts: 1
Reputation: sunil_skmd is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sunil_skmd sunil_skmd is offline Offline
Newbie Poster

Question how to use switch statement in java on strings

  #1  
Aug 30th, 2007
Hi, I am new to java. i want to use a switch statement on operators that are in read as strings. Like "==","!=",">=","<=".

Please help me to do this as i cannot use even their ascii value of these operators to switch the cases.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,660
Reputation: Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light 
Rep Power: 11
Solved Threads: 262
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: how to use switch statement in java on strings

  #2  
Aug 31st, 2007
Originally Posted by sunil_skmd View Post
Hi, I am new to java. i want to use a switch statement on operators that are in read as strings. Like "==","!=",">=","<=".

Please help me to do this as i cannot use even their ascii value of these operators to switch the cases.

Switch statements can only be used on ints or enums. For strings you are stuck with if-else-if blocks or stucturing your code in such as way as to use subclasses to provide the behavior instead of a switch.

That said, if you really want a switch, you can provide a string switching capability with an enum if you code it right. Here's an example just for giggles
import java.util.HashMap;
import java.util.Map;

public enum Operator {
    EQUALS("=="),
    NOT_EQUALS("!="),
    GREATER_EQUALS(">="),
    LESS_EQUALS("<=");
    
    private final String token;
    private static Map<String,Operator> tokenMap;
    
    private Operator(String token){
        this.token = token;
        map(token,this);
    }
    
    private static void map(String token, Operator op){
        if (tokenMap==null) tokenMap = new HashMap<String,Operator>();
        tokenMap.put(token,op);
    }
    
    public static Operator forToken(String token){
        return tokenMap.get(token);
    }
    
    public static void main(String[] args) {
        String[] operators = new String[]{"<=","==","!=",">="};
        for (String opString : operators){
            Operator op = Operator.forToken(opString);
            switch (op) {
                case EQUALS: 
                    System.out.println("equals"); 
                    break;
                case NOT_EQUALS: 
                    System.out.println("not equals"); 
                    break;
                case GREATER_EQUALS: 
                    System.out.println("greater than or equal to"); 
                    break;
                case LESS_EQUALS: 
                    System.out.println("less than or equal to");
                    break;
            }
        }
    }
}

For your case though, you may as well just use an if-else block.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 6:52 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC