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.

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.

hi.. can you help me fixing my program..

import java.io.*;
public class Vowels
{
    public static void main (String args[])throws IOException 
    {
        BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
        char [] Vowel= {a, e, i, o, u,} ;
        int j,k;


        System.out.println("\tT H E  V O W E L S ");
        System.out.println("\t   (A E I O U)");
        System.out.println();


        System.out.print("Please enter a phrase: ");
        String phrase = input.readLine();
        System.out.println();


        char[] charray1 = phrase.toCharArray();
        char[] charray2 = Vowel.toCharArray();

        for(j = 0; j<charray1.length;j++)
        {

        for(k = 0; k<charray2.length;k++)

        {
            if(charray1[i]==charray2[k])
            {
                    switch(charray1[i])
                    {
                        case 1: System.out.print("A");break;
                        case 2: System.out.print("E");break;
                        case 3: System.out.print("I");break;
                        case 4: System.out.print("O");break;
                        case 5: System.out.print("U");break;
                        default: System.out.println(" ");
                    }


                }   
                System.out.print(charray1[i]);  break;  
        }   
    }       
   }
 }

Edited 3 Years Ago by Nick Evan: Fixed formatting

i badly need this before july 21,

Start a new post, use code tags, and give more explanations. What is wrong with your code? What is your question?

This article has been dead for over six months. Start a new discussion instead.