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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:05:58+00:00 2026-05-27T07:05:58+00:00

I’m having trouble getting the input for a 2-d array for an assignment. Basically

  • 0

I’m having trouble getting the input for a 2-d array for an assignment. Basically I have to create a bug that crawls across the screen and writes an ASCII image of our name. We have to get the input from the text file so i figured the best course of action would be to create a 2-d array for each character in a file and have it determine what it does depending on what character is in each spot. However It always shows that the 2-d array has the same contents (shown below)

[[C@b1c5fa

Below is an example of the class, the tester class, and an example of the txt file. How can i get it to show the correct input?

Bug Class

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

public class Bug
{  

   private int startingPoint;
   private char mrBug;
    private char placeholder;
    private int postition;
    private int matrixLength;
    private int matrixRows;
    private String lineGet;
    private String txtFile;
    private char[][] Data = new char[3][];


   /**
      Constructs a computer class with title, days, time and room
   */
   public Bug(int initialPosition, char bug, String inputFile)
   {   
     startingPoint = initialPosition;
     mrBug = bug; 
      txtFile = inputFile;
   }


          public void matrixPrinter()
   {   
    for(int row = 0; row < Data.length; row++)
        {
            for(int col = 0; col < Data[row].length; col++)
            {
            System.out.print(Data[row][col]);
            }
            System.out.print("\n");
        }

   }//End of matrixBuilder Method

    public void matrixBuilder()
   {   
        Scanner in = new Scanner(txtFile);
        matrixRows = 0;
        while (in.hasNextLine())
            {
                lineGet = in.next();
                matrixLength = lineGet.length();
                Data[matrixRows] = new char[matrixLength];
                for(int i = 0; i < matrixLength; i++)
                {
                    placeholder = lineGet.charAt(i);
                    Data[matrixRows][i]= placeholder;

                }//End of For
                matrixRows++;
            }//End of While
        in.close();
   }//End of matrixBuilder Method

    /**
      Gets the title
      @return the title
   */
    public void turn()
   {   
        //return title;

   }

   public void move()
   {   
      // your work here
   }

   /**
      Gets Postition
      @return the postition
   */
   public int getPostion()
   {   
    return postition;

   }
}

http://pastebin.com/g9LWFyXQ

Bug Tester

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

public class BugTester
{
   public static void main(String[] args)
   {
        int start = 0;
       char bugSymbol = 'a';
        String inputFile = "peter.txt";
      Bug crawler1 = new Bug(start,bugSymbol,inputFile);
          crawler1.matrixBuilder();
        crawler1.matrixPrinter();

   }
}

Txt file:

/#****#****#*****#****#****#****#\
/#*##*#*######*###*####*##*#*##*#\
/#****#****###*###****#****####*#\
/#*####*######*###*####*#*#####*#\
/#*####****###*###****#*##*####*#\
  • 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-05-27T07:05:59+00:00Added an answer on May 27, 2026 at 7:05 am

    First: One mistake is in your matrixBuilder() method.

    You init Scanner, passing name of your file to the constructor:

    Scanner in = new Scanner(txtFile);
    

    So it doesn’t read the file contents when you call:

    in.next();
    

    The lineGet variable has the “peter.txt” value. It’s obviously not what you want.

    You need to init Scanner that way:

        Scanner in = null;
        try {
            in = new Scanner(new FileInputStream(txtFile));
        } catch (FileNotFoundException ex) {
            // work up exception
        }
    

    Or just

    public void matrixBuilder() throws FileNotFoundException {
        Scanner in = new Scanner(new FileInputStream(txtFile));
    //...
    }
    

    Second:
    The initial size of your Data array is incorrect:

    private char[][] Data = new char[3][];
    

    Your file “peter.txt” has at least 5 lines.
    So, the initial size of your Data array should also be 5.

    After correcting this mistakes you should get the desired result.

    Hope this helps.

    UPDATE:

    Complete working code:

    Bug.java

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class Bug {
        private int startingPoint;
        private char mrBug;
        private char placeholder;
        private int postition;
        private int matrixLength;
        private int matrixRows;
        private String lineGet;
        private String txtFile;
        private char[][] Data = new char[5][];
    
        /**
        Constructs a computer class with title, days, time and room
         */
        public Bug(int initialPosition, char bug, String inputFile) {
            startingPoint = initialPosition;
            mrBug = bug;
            txtFile = inputFile;
        }
    
        public void matrixPrinter() {
            System.out.println("Data:");
    
            for (int row = 0; row < Data.length; row++) {
                for (int col = 0; col < Data[row].length; col++) {
                    System.out.print(Data[row][col]);
                }
                System.out.print("\n");
            }
    
        }//End of matrixBuilder Method
    
        public void matrixBuilder() throws FileNotFoundException {
            Scanner in = new Scanner(new FileInputStream(txtFile));
    
            matrixRows = 0;
            // We should also check that the number of lines in the file 
            // doesn't exceed the Data array size.
            while (in.hasNextLine() && matrixRows < Data.length) {
                lineGet = in.next();
                System.out.println("line["+ matrixRows + "]:" + lineGet);
                matrixLength = lineGet.length();
                Data[matrixRows] = new char[matrixLength];
                for (int i = 0; i < matrixLength; i++) {
                    placeholder = lineGet.charAt(i);
                    Data[matrixRows][i] = placeholder;
    
                }//End of For
                matrixRows++;
            }//End of While
            in.close();
        }//End of matrixBuilder Method
    
        /**
        Gets the title
        @return the title
         */
        public void turn() {
            //return title;
        }
    
        public void move() {
            // your work here
        }
    
        /**
        Gets Postition
        @return the postition
         */
        public int getPostion() {
            return postition;
    
        }
    }
    

    BugTester.java

    import java.io.FileNotFoundException;
    
    public class BugTester {
    
        public static void main(String[] args) throws FileNotFoundException {
            int start = 0;
            char bugSymbol = 'a';
            String inputFile = "peter.txt";
            Bug crawler1 = new Bug(start, bugSymbol, inputFile);
            crawler1.matrixBuilder();
            crawler1.matrixPrinter();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.