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;
}
}
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:
/#****#****#*****#****#****#****#\
/#*##*#*######*###*####*##*#*##*#\
/#****#****###*###****#****####*#\
/#*####*######*###*####*#*#####*#\
/#*####****###*###****#*##*####*#\
First: One mistake is in your
matrixBuilder()method.You init Scanner, passing name of your file to the constructor:
So it doesn’t read the file contents when you call:
The
lineGetvariable has the “peter.txt” value. It’s obviously not what you want.You need to init Scanner that way:
Or just
Second:
The initial size of your Data array is incorrect:
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
BugTester.java