Question

Why can't the 'length' be resolved? import java.util.*; enum Commands { UNKNOWN, QUIT, LOOK, NORTH, SOUTH,...

Why can't the 'length' be resolved?

import java.util.*;

enum Commands {
    UNKNOWN,
    QUIT,
    LOOK,
    NORTH,
    SOUTH,
    EAST,
    WEST
}

public class Main {

    public static void main(String[] args) {
        System.out.println("Welcome to Zork!");
        SpawnPlayer();
        Scanner scanner = new Scanner(System.in);

        Commands command = Commands.UNKNOWN;

        while (command != Commands.QUIT) {
            System.out.println(rooms[]locationRow[locationColumn]);
            System.out.print(">");
            String inputString = scanner.nextLine();
            command = ToCommand(inputString);

            switch (command)
            {
                case QUIT:
                    System.out.println("Thank you for playing!");
                    break;

                case LOOK:
                    System.out.println("A rubber mat saying 'Welcome to Zork!' lies by the door.");
                    break;
                // North, South, East and West have all the same behaviors
                case NORTH:
                case SOUTH:
                case EAST:
                case WEST:
                    // Move command passes the command to the method to navigate throughout the maze
                    Move(command);
                    break;

                default:
                    System.out.println("Unknown command.");
                    break;
            }
        }
    }

    private static Commands ToCommand(String commandString) {
        try {
            return Commands.valueOf(commandString.trim().toUpperCase());
        } catch (IllegalArgumentException ex) {
            return Commands.UNKNOWN;
        }
    }
    // New Move method that uses commands to navigate throughout the maze
    // uses a boolean method IsDirection which passes back
    private static void Move(Commands command) {
        if (IsDirection(command) == false)
        {
            throw new IllegalArgumentException();
        }

        boolean isValidMove;
        switch (command)
        {
            case NORTH:
                if (locationRow > 0) {
                    isValidMove = true;
                    locationRow--;
                }
                break;

            case SOUTH:
                if (locationRow < rooms[locationColumn].length - 1) {
                    isValidMove = true;
                    locationRow++;
                }
            case EAST:
                if (locationColumn < rooms.length - 1) {
                    isValidMove = true;
                    locationColumn++;
                }
                else
                {
                    isValidMove = false;
                }
                break;

            case WEST:
                if (locationColumn > 0) {
                    isValidMove = true;
                    locationColumn--;
                }
                else
                {
                    isValidMove = false;
                }
                break;

            default:
                isValidMove = false;
                break;
        }

        if (isValidMove == false)
        {
            System.out.println("The way is blocked!");
        }
    }
    // IsDirection method to process the command
    private static boolean IsDirection(Commands command)
    {
        return directions.contains(command);
    }
    // Final Static array which contains the rooms
    // Foyer is at poistion 0

    private static void SpawnPlayer(){
        Random random = new Random();
        locationRow = random.nextInt(rooms.length);
        System.out.println("Location1: " + locationRow);
        locationRow = random.nextInt(rooms[0].length);
        System.out.println("Location1: " + locationRow);
    }
    private static final String[] rooms = {
            "Foyer",
            "Kitchen",
            "Living Room",
            "Dining Room",
            "Bedroom"
    };
    // Sets the starting room location to 0 which is the Foyer
    private static int locationColumn = 0;
    private static int locationRow = 0;

    // Array with a list of valid commands
    private static final ArrayList directions = new ArrayList(Arrays.asList(
            Commands.NORTH,
            Commands.SOUTH,
            Commands.EAST,
            Commands.WEST));
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.util.*;

enum Commands {
    UNKNOWN,
    QUIT,
    LOOK,
    NORTH,
    SOUTH,
    EAST,
    WEST
}

public class Main {

    public static void main(String[] args) {
        System.out.println("Welcome to Zork!");
        SpawnPlayer();
        Scanner scanner = new Scanner(System.in);

        Commands command = Commands.UNKNOWN;

        while (command != Commands.QUIT) {
            System.out.println(rooms[locationRow].charAt(locationColumn));
            System.out.print(">");
            String inputString = scanner.nextLine();
            command = ToCommand(inputString);

            switch (command) {
                case QUIT:
                    System.out.println("Thank you for playing!");
                    break;

                case LOOK:
                    System.out.println("A rubber mat saying 'Welcome to Zork!' lies by the door.");
                    break;
                // North, South, East and West have all the same behaviors
                case NORTH:
                case SOUTH:
                case EAST:
                case WEST:
                    // Move command passes the command to the method to navigate throughout the maze
                    Move(command);
                    break;

                default:
                    System.out.println("Unknown command.");
                    break;
            }
        }
    }

    private static Commands ToCommand(String commandString) {
        try {
            return Commands.valueOf(commandString.trim().toUpperCase());
        } catch (IllegalArgumentException ex) {
            return Commands.UNKNOWN;
        }
    }

    // New Move method that uses commands to navigate throughout the maze
    // uses a boolean method IsDirection which passes back
    private static void Move(Commands command) {
        if (IsDirection(command) == false) {
            throw new IllegalArgumentException();
        }

        boolean isValidMove = false;
        switch (command) {
            case NORTH:
                if (locationRow > 0) {
                    isValidMove = true;
                    locationRow--;
                }
                break;

            case SOUTH:
                if (locationRow < rooms[locationColumn].length() - 1) {
                    isValidMove = true;
                    locationRow++;
                }
            case EAST:
                if (locationColumn < rooms.length - 1) {
                    isValidMove = true;
                    locationColumn++;
                } else {
                    isValidMove = false;
                }
                break;

            case WEST:
                if (locationColumn > 0) {
                    isValidMove = true;
                    locationColumn--;
                } else {
                    isValidMove = false;
                }
                break;

            default:
                isValidMove = false;
                break;
        }

        if (isValidMove == false) {
            System.out.println("The way is blocked!");
        }
    }

    // IsDirection method to process the command
    private static boolean IsDirection(Commands command) {
        return directions.contains(command);
    }
    // Final Static array which contains the rooms
    // Foyer is at poistion 0

    private static void SpawnPlayer() {
        Random random = new Random();
        locationRow = random.nextInt(rooms.length);
        System.out.println("Location1: " + locationRow);
        locationRow = random.nextInt(rooms[0].length());
        System.out.println("Location1: " + locationRow);
    }

    private static final String[] rooms = {
            "Foyer",
            "Kitchen",
            "Living Room",
            "Dining Room",
            "Bedroom"
    };
    // Sets the starting room location to 0 which is the Foyer
    private static int locationColumn = 0;
    private static int locationRow = 0;

    // Array with a list of valid commands
    private static final ArrayList directions = new ArrayList(Arrays.asList(
            Commands.NORTH,
            Commands.SOUTH,
            Commands.EAST,
            Commands.WEST));
}
Add a comment
Know the answer?
Add Answer to:
Why can't the 'length' be resolved? import java.util.*; enum Commands { UNKNOWN, QUIT, LOOK, NORTH, SOUTH,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • *JAVA* Can somebody take a look at my current challenge? I need to throw a different...

    *JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> {    private int top, size;    private E arrS[];    private static final int MAX_STACK_SIZE = 10;    public ArrayStack() {        this.arrS = (E[]) new Object[MAX_STACK_SIZE];        this.top = size;        this.size = 0;...

  • ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*;...

    ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class DrawingPanel implements ActionListener { public static final int DELAY = 50; // delay between repaints in millis private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save"; private static String TARGET_IMAGE_FILE_NAME = null; private static final boolean PRETTY = true; // true to anti-alias private static boolean DUMP_IMAGE = true; // true to write DrawingPanel to file private int width, height; // dimensions...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • Original question: I need a program that simulates a flower pack with the traits listed below:...

    Original question: I need a program that simulates a flower pack with the traits listed below: - You must use a LinkedList for storage (not an Arrray list). - You must be able to display, add, remove, sort, filter, and analyze information from our flower pack. - The flower pack should hold flowers, weeds, fungus, and herbs. All should be a subclass of a plant class. - Each subclass shares certain qualities (ID, Name, and Color) – plant class -...

  • Java Programming: Math Quiz

    Starting codes:MathQuiz.javaimport java.util.Scanner;public class MathQuiz {    private final int NUMBER_OF_QUESTIONS = 10;    private final MathQuestion[] questions = new MathQuestion[NUMBER_OF_QUESTIONS];    private final int[] userAnswers = new int[NUMBER_OF_QUESTIONS];    public static void main(String[] args) {        MathQuiz app = new MathQuiz();        System.out.println(app.welcomeAndInstructions());        app.createQuiz();        app.administerQuiz();        app.gradeQuiz();    }    private String welcomeAndInstructions() {        return "Welcome to Math Quiz!\n" +         ...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT