Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:23:16+00:00 2026-06-11T22:23:16+00:00

I am making a program which takes in data from a file and makes

  • 0

I am making a program which takes in data from a file and makes a maze game from it. An example maze.txt file would look like:

5  5
P.XX.
...X.
.XT..
..X..
X....

Where the two numbers on top define the rows and columns of the array.
Here is the code I am using

import java.util.*;
import java.io.*;

public class MazeGame {

    public static void main(String[] args) throws Exception {

        // Display the maze

        Scanner sc = new Scanner(new File("maze.txt"));
        int rows1 = sc.nextInt();
        int columns1 = sc.nextInt();
        rows1 += 1;
        columns1 += 1;

        BufferedReader in = new BufferedReader(new FileReader("maze.txt"));

        char[][] treasureMaze = new char[rows1][columns1];
        for (int i = 0; i < rows1 || i < columns1; ++i) {
            String line = in.readLine();
            if (line == null) {
                System.out.println("Error in array");
                return;
            }
            sc.nextLine();

            treasureMaze[i] = line.toCharArray();
        }

        display(treasureMaze);
        int vertical = 0;
        int horizontal = 0;

        // Give Move Options
        options();

        // Setup a while loop that continues until
        // the user has gotten to the treasure, or 'P'
        while (treasureMaze[vertical][horizontal] != 'T') {
            // Get Users Decision
            Scanner moveChoice = new Scanner(System.in);
            int choice = moveChoice.nextInt();

            if (choice == 1) {
                System.out.println("You chose to Move up");
            } else if (choice == 2) {
                System.out.println("You chose to Move down");
            } else if (choice == 3) {
                System.out.println("You chose to Move left");
            } else if (choice == 4) {
                System.out.println("you chose to Move right");
            } else {
                return;
            }

            // Move the Player: Each choice will move the player
            // according to their choice and then re-display the
            // map and options so that they can move through the maze

            // Move Up
            if (choice == 1) {
                if (vertical - 1 < 0) {
                    System.out
                            .println("\nCannot move there! Try something else\n");
                    display(treasureMaze);
                    options();
                } else if (treasureMaze[vertical - 1][horizontal] == '.') {
                    treasureMaze[vertical - 1][horizontal] = 'P';
                    treasureMaze[vertical][horizontal] = '.';
                    vertical -= 1;
                    display(treasureMaze);
                    options();
                } else if (treasureMaze[vertical - 1][horizontal] == 'T') {
                    System.out.println("\nCongratulations you won!");
                    treasureMaze[vertical][horizontal] = 'T';
                } else {
                    System.out
                            .println("\nCannot move there! Try something else\n");
                    display(treasureMaze);
                    options();
                }
            }

            // Move Down
            else if (choice == 2) {
                if (vertical + 1 < 0) {
                    System.out
                            .println("\nCannot move there! Try something else\n");
                    display(treasureMaze);
                    options();
                } else if (treasureMaze[vertical + 1][horizontal] == '.') {
                    treasureMaze[vertical + 1][horizontal] = 'P';
                    treasureMaze[vertical][horizontal] = '.';
                    vertical += 1;
                    display(treasureMaze);
                    options();
                } else if (treasureMaze[vertical + 1][horizontal] == 'T') {
                    System.out.println("\nCongratulations you won!");
                    treasureMaze[vertical][horizontal] = 'T';
                } else {
                    System.out
                            .println("\nCannot move there! Try something else\n");
                    display(treasureMaze);
                    options();
                }
            }

            // Move Left
            else if (choice == 3) {
                if (horizontal - 1 < 0) {
                    System.out
                            .println("\nCannot move there! Try something else\n");
                    display(treasureMaze);
                    options();
                } else if (treasureMaze[vertical][horizontal - 1] == '.') {
                    treasureMaze[vertical][horizontal - 1] = 'P';
                    treasureMaze[vertical][horizontal] = '.';
                    horizontal -= 1;
                    display(treasureMaze);
                    options();
                } else if (treasureMaze[vertical][horizontal - 1] == 'T') {
                    System.out.println("\nCongratulations you won!");
                    treasureMaze[vertical][horizontal] = 'T';
                } else {
                    System.out
                            .println("\nCannot move there! Try something else\n");
                    display(treasureMaze);
                    options();
                }
            }

            // Move Right
            else if (choice == 4) {
                if (horizontal + 1 < 0) {
                    System.out
                            .println("\nCannot move there! Try something else\n");
                    display(treasureMaze);
                    options();
                } else if (treasureMaze[vertical][horizontal + 1] == '.') {
                    treasureMaze[vertical][horizontal + 1] = 'P';
                    treasureMaze[vertical][horizontal] = '.';
                    horizontal += 1;
                    display(treasureMaze);
                    options();
                } else if (treasureMaze[vertical][horizontal + 1] == 'T') {
                    System.out.println("\nCongratulations you won!");
                    treasureMaze[vertical][horizontal] = 'T';
                } else {
                    System.out
                            .println("\nCannot move there! Try something else\n");
                    display(treasureMaze);
                    options();
                }
            } else {
                System.out.println("Bye!");
                return;
            }

        }
    }

    // Display Object: prints out the maze for the user
    public static void display(char x[][]) {
        for (int row = 0; row < x.length; row++) {
            for (int column = 0; column < x[row].length; column++) {
                System.out.print(x[row][column] + "\t");
            }
            System.out.println();
        }
    }

    // Options Object: gives the options menu to the user
    static void options() {
        System.out.println("You may:");
        System.out.println("\t1) Move up");
        System.out.println("\t2) Move down");
        System.out.println("\t3) Move left");
        System.out.println("\t4) Move right");
        System.out.println("\t0) Quit");

    }
}

When I try to run the program, I need to see the 5 5 for the first run and then get rid of it so that I can use the rest of my program without running into the numbers on top. Is there a way to ignore these numbers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:23:17+00:00Added an answer on June 11, 2026 at 10:23 pm

    You could just use Scanner.nextLine Instead of opening the file again with a BufferedReader.

    EDIT:

    To clarify, I mean:

        Scanner sc = new Scanner(new File("maze.txt"));
        int rows1 = sc.nextInt();
        int columns1 = sc.nextInt();
    
        char[][] treasureMaze = new char[rows1][columns1];
    
        for (int i = 0; i < rows1; ++i) {
            String line = sc.nextLine();
    

    EDIT2:

    I removed

        rows1 += 1;
        columns1 += 1;
    

    and changed the for loop:

    I’m not sure what the purpose of adding 1 to rows and columns was and the loop should really only loop over the number of rows rows1. At least for the reading in the file part.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a timetabling program which does one to one matches from SubjectTeacherPeriod
I'm starting a new project which would greatly benefit from program add-ons. The program
I am making a program that takes input from files that I have called
I am making a basic program which takes some input and prints it back
I'm making a program which sends data to many TCP listeners. If one of
I am making a program which has to check a data base once on
I am currently making a program which is supposed to prompt the user to
So I'm making a program which is supposed to print a horizontal histogram of
I'm making a program which create a RAW socket in order to read all
I am making a program with which to save banking information (i.e., account, password,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.