#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2003
    Posts
    595
    Rep Power
    0

    Whats wrong with this switch statement


    I keep geting this error
    C:\irfan_java>javac switching.java
    switching.java:8: incompatible types
    found : java.lang.String
    required: int
    switch ( name )
    ^
    1 error
    *************
    What am I doing wrong here?


    *********************
    Code:
    class  switching
    
    
    
    {
        public static void main(String[] args) 
        {
            
        String name= "paul";
        
         switch ( name )
            {
            case "paul": 
            System.out.println("name is paul");
            break;
            
            case "sam":
             System.out.println("sam");
             break;
    
            default:
             System.out.println("no name found");
            } 
        
        
        
        }
    }
    web developer
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,040
    Rep Power
    1291
    Originally Posted by isheikh
    I keep geting this error
    C:\irfan_java>javac switching.java
    switching.java:8: incompatible types
    found : java.lang.String
    required: int
    switch ( name )
    ^
    1 error
    *************
    What am I doing wrong here?


    *********************
    Code:
    class  switching
    
    
    
    {
        public static void main(String[] args) 
        {
            
        String name= "paul";
        
         switch ( name )
            {
            case "paul": 
            System.out.println("name is paul");
            break;
            
            case "sam":
             System.out.println("sam");
             break;
    
            default:
             System.out.println("no name found");
            } 
        
        
        
        }
    }
    You can't use a String as a switch variable.
  4. #3
  5. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    Just to expand on bullet's reply, a switch value must be an enum or an integral value. Furthermore, each case value must be "widenable" to the switch value; i.e., you're not allowed to switch on a short and use int values for the cases.

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2003
    Posts
    595
    Rep Power
    0
    thanks ....so u saying switch can not be used for "text" , it can be only used for numbers?

    I found this example on one of the websites, i guess thats wrong too...........

    Code:
    String userCommand = input.readLine();
    
    	switch (userCommand)
    	{
    		case "read":
    			read();
    			break;
    
    		case "readLine":
    			readLine();
    			break;
    
    		case "write":
    			write();
    			break;
    
    		case "dir":
    			dir();
    			break;
    
    		case "quit":
    			quit();
    			break;
    
    		default:
    			unknownCommand();
    	}
    web developer
  8. #5
  9. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,040
    Rep Power
    1291
    Which website?
  10. #6
  11. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    thanks ....so u saying switch can not be used for "text" , it can be only used for numbers?
    No. I am saying that switch cannot be used with strings or other objects aside from enums; it can only be used with enums and integral values ([EDIT] and their wrappers, thanks to autoboxing). Other numeric values (e.g., long, float, double) won't work, either.

    I found this example on one of the websites, i guess thats wrong too...........
    Please .................... don't................... write...................like.............this..........

    And yes, the example you posted is wrong (same as the first one). Your compiler has told you this.

    The Java Tutorial - The switch Statement

    ~
    Last edited by Yawmark; March 28th, 2007 at 02:09 PM.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  12. #7
  13. Meow Black Belt
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Location
    Beaverton OR
    Posts
    864
    Rep Power
    531
    Originally Posted by bullet
    Which website?
    A STFW suggests that this comes from a site that suggests changes to switch statements to make it more useful. The site doesn't give it out as a working code. Maybe the OP should read the reference more carefully, especially the first line
    This proposal suggests changes to make the Java switch statement more useful.

    Comments on this post

    • wsa1971 agrees
  14. #8
  15. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    There's always Groovy...

    Groovy Code:
    def x = 1.23
    def result = ""
     
    switch ( x ) {
        case "foo":
            result = "found foo"
            // lets fall through
     
        case "bar":
            result += "bar"
     
        case [4, 5, 6, 'inList']:
            result = "list"
            break
     
        case 12..30:
            result = "range"
            break
     
        case Integer:
            result = "integer"
            break
     
        case Number:
            result = "number"
            break
     
        default:
            result = "default"
    }
     
    assert result == "number"




    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

IMN logo majestic logo threadwatch logo seochat tools logo