Results 1 to 3 of 3
  1. #1
    baltimore is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Problem with a switch statement in Java

    I've been having this odd problem with a switch statement in Java.

    The problem I'm having is that the switch statement doesn't seem to be recognizing the correct input. As an example, I'm checking for various characters, and i read them in as ints. The ASCII code for a ':' is 58, and when I test it like this:

    Java Code:
    if( input == 58){
    do stuff;
    }
    it works fine, but the same input in a switch statement doesn't do anything:
    Java Code:
    switch( input){
    case 58:
    do stuff:
    break;
    }
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    According to this test/exploration your code should work. I wonder if the variable input could be something other than a char.
    Java Code:
    public class Test {
        public static void main(String[] args) {
            char[] chars = { 'a', 'b', 'c', 'd' };
            explore(chars);
            String s = "";
            for(int j = 0; j < chars.length; j++) {
                char c = chars[j];
                switch(c) {
                    case 'a':     // both seem to
    //                case 97:    // work okay
                        s = "a";
                        break;
                    case 'b':
    //                case 98:
                       s = "b";
                       break;
                    case 'c':
    //                case 99:
                       s = "c";
                       break;
                    case 'd':
    //                case 100:
                       s = "d";
                       break;
                    default:
                       s = "default";
                }
                System.out.printf("j = %d  s = %s%n", j, s);
            }
        }
    
        private static void explore(char[] chars) {
            for(int j = 0; j < chars.length; j++) {
                char c = chars[j];
                int n = (int)chars[j];
                System.out.printf("j = %d  c = %s  n = %3d  c == n = %b%n",
                                   j, c, n, c == n);
            }
        }
    }

  3. #3
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Maybe cuz of the typo where u use ':' instead of the ';'
    The char could be used as in input when i tested it, input, bytes, chars, all available i think

Similar Threads

  1. Method in a Switch Statement
    By cart1443 in forum New To Java
    Replies: 6
    Last Post: 03-14-2008, 03:48 AM
  2. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM
  3. Replies: 0
    Last Post: 01-04-2008, 09:32 AM
  4. JavaScript to Java Statement
    By mutuah in forum Advanced Java
    Replies: 0
    Last Post: 08-08-2007, 05:16 AM
  5. Help with gigantamous switch statement
    By trill in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •