简体   繁体   中英

How to break a String into a string array, and check if the string is one thing or another

So this is a bit vauge, but i have a program that is suposed to be a texed based adventure game. My code workes ok so far but i cant get my command to move from room to room to work. Here are my three relevent classes:

http://pastebin.com/uapycAZF

But I want to focus on this part:

// takes the user's input and puts it into a string array String[] parts = input.split("\\\\s+");

//if the user inputs equals go
    else if (parts.length == 2 && parts[0].equalsIgnoreCase("go"))
    {
        //if the second part of the input is nth
        if(parts[1].equalsIgnoreCase("nth"))
        {
            //gets the room the player is in
            Room temp = getRoomLoc(player.getLocation());
            //if the player can move north
            if(temp.canMoveNth())
            {
                //sets the player's location to the room to the north
                player.setLocation(temp.getNth());
            }
            //if the player can't move north this checks why
            if(!temp.canMoveNth())
            {
                //if there is a door to the north
                if(temp.getNth()>-1)
                {
                    //checks if the door is locked
                    if(temp.getRoomlock().NthLocked)
                    {
                        //this code shouldn't run if there isn't a door
                       System.out.println("The Door to the north is "
                               + "locked");
                    }
                    else
                    {
                        //if this runs something is FUBAR
                        System.out.println("Error, you broke it: North");
                    }
                }
                //if there isn't a door to the north
                else
                {
                    System.out.print("There is no door to the north.");
                }
            }
        }
        //if the second part of the player's input is south
        else if(parts[1].equalsIgnoreCase("sth"))
        {
            //gets the player's location
             Room temp = getRoomLoc(player.getLocation());
             //if the player can move south
            if(temp.canMoveSth())
            {
                //sets the players location to the south
                player.setLocation(temp.getSth());
            }
            //if the player can't move south
            if(!temp.canMoveSth())
            {
                //if there is a door
                if(temp.getSth()>-1)
                {
                    //checks if it is locked
                    if(temp.getRoomlock().SthLocked)
                    {
                        //this code shouldn't run if there isn't a door
                       System.out.println("The Door to the south is "
                               + "locked");
                    }
                    else
                    {
                        //if this runs something is FUBAR
                        System.out.println("Error, you broke it: South");
                    }
                }
                //if there isn't a door
                else
                {
                    System.out.print("There is no door to the south.");
                }
            }
        }
        //if the second part of the player's input is east
        else if(parts[1].equalsIgnoreCase("est"))
        {
            //gets the the player's location
             Room temp = getRoomLoc(player.getLocation());
             //if the player can move east
            if(temp.canMoveEst())
            {
                //sets the player's location to the east
                player.setLocation(temp.getEst());
            }
            //if the player can't move east
            if(!temp.canMoveEst())
            {
                //if there is a door
                if(temp.getEst()>-1)
                {
                    //checks if it is locked
                    if(temp.getRoomlock().EstLocked)
                    {
                        //this code shouldn't run if there isn't a door
                       System.out.println("The Door to the east is "
                               + "locked");
                    }
                    else
                    {
                        //if this runs something is FUBAR
                        System.out.println("Error, you broke it: East");
                    }
                }
                //if there is no door
                else
                {
                    System.out.print("There is no door to the east.");
                }
            }
        }
        //if the second part of the player's input is west
        else if(parts[1].equalsIgnoreCase("wst"))
        {
            //gets the player's location
         Room temp = getRoomLoc(player.getLocation());
         //if the player can move west
            if(temp.canMoveWst())
            {
                //sets the player's location to the west
                player.setLocation(temp.getSth());
            }
            //if the player can't move west
            if(!temp.canMoveWst())
            {
                //if there is a door
                if(temp.getWst()>-1)
                {
                    //checks if it is locked
                    if(temp.getRoomlock().SthLocked)
                    {
                        //this code shouldn't run if there isn't a door
                       System.out.println("The Door to the west is "
                               + "locked");
                    }
                    else
                    {
                        //if this runs something is FUBAR
                        System.out.println("Error, you broke it: West");
                    }
                }
                //if there is no door
                else
                {
                    System.out.print("There is no door to the west.");
                }
            }    
        }

        else
        {
            System.out.println("\""+ parts[1] +"\""+" is not a valid "
                    + "direction");
        }

I want it to detect when the first part of the input is "go" and the second is "nth/sth/est/wst" and my other comands work but whith this command it either doesn't detect "go" or it gives my array an out of bounds exception when i call "parts[1]" but its length is 2?

What about using input.starsWith("go") and input.endsWith("nth/sth/est/wst") ?

Java has a bunch of great objects like String that have a lot of useful methods already there for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM