二维阵列没有正确输入

时间:2011-12-05 06:24:25

标签: java multidimensional-array

我无法获得分配的二维数组的输入。基本上我必须创建一个在屏幕上爬行并写入我们名字的ASCII图像的错误。我们必须从文本文件中获取输入,因此我认为最好的做法是为文件中的每个字符创建一个二维数组,并根据每个点中的字符确定它的作用。但是它始终显示2-d数组具有相同的内容(如下所示)

[[C@b1c5fa

下面是类的示例,测试器类和txt文件的示例。如何让它显示正确的输入?

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文件:

/#****#****#*****#****#****#****#\
/#*##*#*######*###*####*##*#*##*#\
/#****#****###*###****#****####*#\
/#*####*######*###*####*#*#####*#\
/#*####****###*###****#*##*####*#\

2 个答案:

答案 0 :(得分:2)

您所看到的是数组对象的内部表示(其“签名”)。

如果要打印数组,则需要迭代其元素。因为它是2D,只需使用两个嵌套的for循环。

其他问题:

  • 您正在使用Scanner初始化String,但您想要的是使用File对其进行初始化。您需要使用您的文件名创建一个新的File对象,并将其传递给Scanner构造函数,以处理可能的异常
  • 您的文件有四行,但您Data数组只有三行。一旦读取了数组可以容纳的行数,就可以使数组更大,或者从输入循环中解脱出来。

答案 1 :(得分:1)

首先:您的matrixBuilder()方法存在一个错误。

您初始化扫描程序,将文件名传递给构造函数:

Scanner in = new Scanner(txtFile);

因此,当您致电时,它不会读取文件内容:

in.next();

lineGet变量具有“peter.txt”值。这显然不是你想要的。

您需要以这种方式初始化扫描程序:

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

或者只是

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

<强>第二 数据数组的初始大小不正确:

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

您的文件“peter.txt”至少包含 5 行。 因此,数据阵列的初始大小也应 5

纠正这些错误后,你应该得到理想的结果。

希望这会有所帮助。

<强>更新

完整的工作代码:

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();
    }
}