Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Sep 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java switch statement

    I've been having this odd problem with a switch statement in Java. I've been programming academically for several years, but it's been a while since I did any Java.

    Anyway, 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:

    if( input == 58){
    do stuff;
    }

    it works fine, but the same input in a switch statement doesn't do anything:

    switch( input){
    case 58:
    do stuff:
    break;
    }

    Any ideas about what I'm doing wrong.

    Thanks.

  • #2
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You have a : after 'do stuff', should be a ;
    <JPM />

  • #3
    New to the CF scene
    Join Date
    Sep 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, yea that should have been a ;
    but the compiler told me the same thing, and after fixing it, itstill doesn't work.

  • #4
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What error do you get, and are you sure the input is a char? If it is than it must be something else in your code, the switch statement looks good....
    Last edited by JPM; 09-09-2004 at 09:12 PM.
    <JPM />

  • #5
    New to the CF scene
    Join Date
    Sep 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help.

    There is no error, it compiles fine, and works perfectly in the if statement.
    I thought a switch was supposed to do essentially the same thing, but maybe not. I'll try to cast the int as a char and test that.

    thanks again.

  • #6
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe you have to have atleast to cases in the switch statment? Seems weird, though its still logical.
    <JPM />

  • #7
    New to the CF scene
    Join Date
    Sep 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have about 15 cases actually, I was just trying to keep my post brief. I tried casting as a char and no go. This is really confusing me. As far as I can tell, java isn't even testing my input against the case statements. I have no way of telling though, as everything runs fine except for this, and I get no errors due to a catch all method I have at the end.

    I may just have to rewrite the whole thing using if else statements, but thanks for the input.

  • #8
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The following is a correct switch statment. You could try to copy and paste it.

    Code:
    switch (x) {
      case 0: 
        doSomething0();
        break;
      case 1: 
        doSomething1();
        break;
      case 2: 
        doSomething2();
        break;
      case 3: 
        doSomething3();
        break;
      case 4: 
        doSomething4();
        break;
      default: 
        doSomethingElse();
    }
    link
    Last edited by JPM; 09-09-2004 at 09:25 PM.
    <JPM />

  • #9
    New to the CF scene
    Join Date
    Sep 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It works!.
    I think it had something to do with the default statement. I didn't have one. I didn't think that made a difference, but everythings is working great now. Thanks so much for you help.


  •  

    Posting Permissions

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