使用.txt文件中的对象填充数组?

时间:2021-05-25 21:29:11

I'm new to Java and am trying to create an array with objects read from a .txt file. The file looks something like this

我是Java的新手,我正在尝试创建一个包含从.txt文件读取的对象的数组。该文件看起来像这样

Wall 2 2
Wall 3 4
Wall 3 5

.... and so on.

.... 等等。

What I want to do is use the RandomAccessFile() function to fill an array[8][8] with the objects in the file, and in their appointed positions. I've been looking around but can't find a solution, or maybe I'm not looking in the right place. Any help will be appreciated.

我想要做的是使用RandomAccessFile()函数用文件中的对象和它们指定的位置填充数组[8] [8]。我一直在环顾四周,但找不到解决方案,或者说我找不到合适的地方。任何帮助将不胜感激。

EDIT:

编辑:

I've made some progress (I think) and am able to read from the .txt file, however, I can't seem to assign objects to specific locations in my array... This is what I've got

我已经取得了一些进展(我认为)并且能够从.txt文件中读取,但是,我似乎无法将对象分配给我的数组中的特定位置...这就是我所拥有的

        public static void leer() throws IOException
{
    Scanner s = new Scanner(new File("init.txt"));
        while (s.hasNext()) 
        {

            if (s.next()=="Wall")
            {
            int i = s.nextInt();
            int j = s.nextInt();
            Tablero[i][j]=new Wall();

            }
            else if (s.next()=="Ghost")
            {
            int i = s.nextInt();
            int j = s.nextInt();
            Tablero[i][j]=new Ghost();
            }
        }
}

Now, I'm getting a "NoSuchElementException", which I gather means I'm not defining the Walls or Ghosts properly, and sadly, I don't quite understand the enum function... Again, any help would be great!

现在,我得到了一个“NoSuchElementException”,我收集的意思是我没有正确定义Walls或Ghosts,遗憾的是,我不太了解enum函数......再次,任何帮助都会很棒!

1 个解决方案

#1


1  

This will work:

这将有效:

Scanner s = new Scanner(new File("map.txt"));
String[][] map = new String[8][8];
while (s.hasNext()) {
    String value = s.next();
    int x = s.nextInt();
    int y = s.nextInt();
    map[x][y] = value;
}

You might want to consider using an Enum to store the item in each cell:

您可能需要考虑使用Enum将项目存储在每个单元格中:

public enum CellType {
    EMPTY, WALL, POWERUP
}

Scanner s = new Scanner(new File("map.txt"));
CellType[][] map = new CellType[8][8];
while (s.hasNext()) {
    String value = s.next().toUpperCase();
    int x = s.nextInt();
    int y = s.nextInt();
    map[x][y] = CellType.valueOf(value);
}

EDIT:

You're calling .next() twice in your code. You need to evaluate it only once, so only one token is consumed:

你在代码中两次调用.next()。您只需要评估一次,因此只消耗一个令牌:

public static void leer() throws IOException {  
    Scanner s = new Scanner(new File("init.txt"));  
    while (s.hasNext()) {
        //Read these at the top, so we don't read them twice, and consume too many tokens 
        String item = s.next();
        int i = s.nextInt();  
        int j = s.nextInt();

        if(item == "Wall") {  
            Tablero[i][j] = new Wall();
        }  
        else if(item =="Ghost") {  
            Tablero[i][j]=new Ghost();  
        }  
    }  
} 

#1


1  

This will work:

这将有效:

Scanner s = new Scanner(new File("map.txt"));
String[][] map = new String[8][8];
while (s.hasNext()) {
    String value = s.next();
    int x = s.nextInt();
    int y = s.nextInt();
    map[x][y] = value;
}

You might want to consider using an Enum to store the item in each cell:

您可能需要考虑使用Enum将项目存储在每个单元格中:

public enum CellType {
    EMPTY, WALL, POWERUP
}

Scanner s = new Scanner(new File("map.txt"));
CellType[][] map = new CellType[8][8];
while (s.hasNext()) {
    String value = s.next().toUpperCase();
    int x = s.nextInt();
    int y = s.nextInt();
    map[x][y] = CellType.valueOf(value);
}

EDIT:

You're calling .next() twice in your code. You need to evaluate it only once, so only one token is consumed:

你在代码中两次调用.next()。您只需要评估一次,因此只消耗一个令牌:

public static void leer() throws IOException {  
    Scanner s = new Scanner(new File("init.txt"));  
    while (s.hasNext()) {
        //Read these at the top, so we don't read them twice, and consume too many tokens 
        String item = s.next();
        int i = s.nextInt();  
        int j = s.nextInt();

        if(item == "Wall") {  
            Tablero[i][j] = new Wall();
        }  
        else if(item =="Ghost") {  
            Tablero[i][j]=new Ghost();  
        }  
    }  
}