Results 1 to 3 of 3

Thread: Switch Issues

  1. #1
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Switch Issues

    Hello!

    I'm having some issues with a switch, or at least I think it's the switch...

    Java Code:
    // STATE is a String, command is a JTextField
    // This is all inside of an ActionListener, so nothing happens until it gets input.
    
    if (STATE.equals("CREATECHAR")){
    			String name = command.getText();
    				display.append("\nAre you sure you want your name to be " + command.getText() + "?");
    				switch (event.getActionCommand().toUpperCase()){
    				case "YES": case "Y":
    					player.setName(name);
    					display.append("\nWelcome to Generica, " + player.getName() + "!");
    					STATE = "EXPLORE";
    					break;
    				case "NO": case "N":
    					display.append("Well what is your name, then?");
    					break;
    			}
    		}
    Theoretically, after the switch receives a "YES", it should change the state String to "EXPLORE", thus ending the whole chain. But that's not what's happening.

    Output is:

    What is your name, Adventurer?
    Are you sure you want your name to be John?
    Are you sure you want your name to be Yes?
    Welcome to Generica, Yes!

    Any idea why I'm getting the odd looping?

    Thanks in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Switch Issues

    Any idea why I'm getting the odd looping?
    Try debugging the code by adding some printlns to print out the values of the variables used to control the logic flow.
    The print out should show you what the computer is seeing and help you understand why the code is doing what it is doing.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Switch Issues

    Oh, brilliant. Now I know exactly what I did wrong. Looks like a nightmare to fix, too. Think I might just scrap it and restructure it to make sense. xD

    Handy tip for future projects, too.

    Thanks, mate!

Similar Threads

  1. Switch
    By java4amanda in forum New To Java
    Replies: 13
    Last Post: 03-21-2012, 10:53 AM
  2. Working with a switch
    By überfuzz in forum New To Java
    Replies: 2
    Last Post: 03-12-2011, 01:47 AM
  3. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 06:32 PM
  4. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 09:46 AM
  5. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 08:25 PM

Posting Permissions

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