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 9014279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:28:12+00:00 2026-06-16T03:28:12+00:00

Creating a small battleship game on Android that will use RMI and Tomcat. I

  • 0

Creating a small battleship game on Android that will use RMI and Tomcat.

I need to load data from a text file “boards.txt” and possibly store it in a HashMap in order to query it.

I cannot attach the file but below is a screenshot of the .txt file in Notepad. It displays the board layout for 10,000 Battleship game boards (I think it’s that number anyway) which are 10×10 grids.

So each 100 characters of the file will be the locations of the different ships and blank spaces for a 10 x 10 grid with each separated by a line break.

What I’m struggling with is the actual parsing. I think saving the data to a HashMap will make it easier to search through for picking a random board layout. Below is the code that I am currently using to read in the data.

public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileReader("C:\\Path\\boards.txt"));

        HashMap<String, String> map = new HashMap<String, String>();

        while (scanner.hasNextLine()) {
            String columns = scanner.nextLine();
            map.put(columns, columns);
        }

        System.out.println(map);
    }

Now the main reason I have it like this was just to get rid of errors. Previously my code was like this:

public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileReader("C:\\Path\\boards.txt"));

        HashMap<String, String> map = new HashMap<String, String>();

        while (scanner.hasNextLine()) {
            String[] columns = scanner.nextLine().split(" ");
            map.put(columns[0], columns[1]);
        }

        System.out.println(map);
    }

But, I was getting an out of bounds exception. Due to the fact there is no blank spaces to split most likely.

OK so my main issue is, how do I either save the entire .txt file to a HashMap and only display one line of 100 characters at a time.

And Secondly (lower priority) – I would like to choose one line at random so I could create a random number between 1-10000 to display one line. I thought of using a String, Integer HashMap and somehow have a index value for each line but I’m not sure how I would do that.

**EDIT
Sorry I forgot to add the screenshot. Here it is:
enter image description here

Thank you for your answers, I’ll read through them now

  • 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-16T03:28:13+00:00Added an answer on June 16, 2026 at 3:28 am

    To answer your questions directly:

    1) You pretty much had your file reader, but you needn’t bother with the Map, a List will suffice.

    Scanner scanner = new Scanner(new FileReader("C:\\Path\\boards.txt"));
    List<String> lines = new ArrayList<String>();
    while (scanner.hasNextLine()) {
        String columns = scanner.nextLine();
        lines.add(columns);
    }
    

    2) To grab a random line, just pick a random index into the List.

    Random random = new Random();
    int randomLineIndex = random.nextInt(lines.size());
    String randomLine = lines.get(randomLineIndex);
    

    Now, all that said, if you don’t need to load the whole file, don’t. If you just need to grab a random line from the file then go right for it and save yourself computation and memory.

    To do so, you’ll first need to known the number of lines in the file.
    This question gives you:

    String file = "C:\\Path\\boards.txt";
    BufferedReader reader = new BufferedReader(new FileReader(file));
    int lineCount = 0;
    while (reader.readLine() != null) lineCount++;
    reader.close();
    

    Now you know how many lines there are and can pick one at random:

    Random random = new Random();
    int randomLineIndex = random.nextInt(lineCount);
    

    Then you can grab that line and ignore the rest:

    reader = new BufferedReader(new FileReader(file));
    int i = 0;
    while (++i < randomLineIndex)
        // Skip all lines before the one we want
        reader.readLine();
    String randomLine = reader.readLine();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a small tool that will fetch new issues from our redmine repository
I'm currently creating a small C# program that inserts data from files into a
I am creating a small tool that reads csv file data which has got
I am creating a small application that will open a word document, scan it
I'm creating a small application that I will distribute via NuGet. My web app
I am creating a small app for personal use that allows me to clean
I am creating a small application that will be deployed on Window. The database
I am creating a small game for students, and in a place, it has
I am creating a small application which will perform say 4 different, time consuming
I am creating a small (for now) stored procedure to insert some data. When

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.