简体   繁体   English

使用Java从文本文件创建2D数组

[英]Create 2D array from text file in Java

I just started learning java and now I'm trying to input a text file and make it a 2-dimensional string array. 我刚刚开始学习Java,现在尝试输入一个文本文件并将其制成二维字符串数组。 But somehow it shows no line found in the output(NoSuchElementException). 但是以某种方式它显示在输出中没有找到任何行(NoSuchElementException)。 So here is my code: 所以这是我的代码:

public class Maze {

final static int Max_Maze_Row = 20;
final static int Max_Maze_Column = 50;
public static String mazearray;

public static void create() throws Exception
{

   Scanner sc = new Scanner(new BufferedReader(new FileReader("Maze.txt")));
   String [][] mazearray = new String[Max_Maze_Row][Max_Maze_Column];
   while(sc.hasNextLine())
   {

       for(int i = 0 ;i<=Max_Maze_Row;i++)
       {
           for(int j = 0 ;j<=Max_Maze_Column;j++)
           {
               mazearray[i][j] = sc.nextLine();
               System.out.println(mazearray[i][j]);
           }
       }
   }
}

public static void display()
{
    System.out.println(Maze.mazearray); 
}

and here is the main method: 这是主要方法:

    public static void main(String[] args) throws Exception
{
    Maze mazeobject = new Maze();
    mazeobject.create();

}

So this is how the text file looks like: Maze.txt I've seen a lot of forum discussing the same questions, but none of them works for mine. 因此,文本文件如下所示: Maze.txt我见过很多论坛都在讨论相同的问题,但是它们都不适用于我的问题。 Thanks in advance! 提前致谢! I really appreciate your help. 非常感谢您的帮助。

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Scanner;

public class Maze {
    final static int Max_Maze_Row = 20;
    public String[] mazearray;

    public void create() throws Exception {

        Scanner sc = new Scanner(new BufferedReader(new FileReader("./Maze.txt")));
        mazearray = new String[Max_Maze_Row];
        int i = 0;
        while (sc.hasNextLine()) {
            mazearray[i++] = sc.nextLine();
        }
    }

    public void display() {
        System.out.println(Arrays.deepToString(mazearray));
    }

    public static void main(String[] args) throws Exception {
        Maze mazeobject = new Maze();
        mazeobject.create();
        mazeobject.display();
    }
}

You have to make sure there are enough lines in the ./Maze.txt as you required by Max_Maze_Row = 20 and Max_Maze_Column = 50 . 你必须确保有足够的线路中的./Maze.txt为您Max_Maze_Row = 20,Max_Maze_Column = 50必需的。 Otherwise this line mazearray[i][j] = sc.nextLine(); 否则,此行mazearray[i][j] = sc.nextLine(); will fail by java.util.NoSuchElementException: No line found 将因java.util.NoSuchElementException: No line found而失败java.util.NoSuchElementException: No line found

Change your create method like this: 像这样更改您的创建方法:

public static void create() throws Exception {
    Scanner sc = new Scanner(new BufferedReader(new FileReader("Maze.txt")));
    String[][] mazearray = new String[Max_Maze_Row][Max_Maze_Column];
    int lineCounter = 0;

    while (sc.hasNextLine()) {
        String data = sc.nextLine();

        for (int i = 0; i < data.length(); i++) {
            mazearray[lineCounter][i] = String.valueOf(data.charAt(i));
            System.out.print(mazearray[lineCounter][i]);
        }

        System.out.println();
        lineCounter++;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM