简体   繁体   English

如何将文件中的字符转换为Java中的2D数组?

[英]How do i get characters in a file into a 2D array in Java?

So i have a file that looks like this: 所以我有一个看起来像这样的文件:

+-+-+-+ ("/n")
|S|   | ("/n")
+ + + + ("/n")
|   |E| ("/n")
+-+-+-+ ("/n")

/n being a new line in the file /n是文件中的新行

and i want to have each character here as an entry in a 5x7 array. 我想在这里将每个字符作为5x7数组中的条目。 I do not know how to do it, this is what i have tried (along with a lot of other things. input is the file): 我不知道该怎么做,这是我尝试过的方法(还有很多其他事情。输入是文件):

public static void main(String[] args) throws FileNotFoundException {

  Scanner input = new Scanner(new File("maze0.txt"));

  char maze[][] = new char[5][7];
  int charCount = 0;
     for (int row = 0; row < finalHeight; row++) {
        for (int col = 0; col < finalWidth; col++) {
           while (input.hasNextLine()){
               String line = input.nextLine();
              if ((row < finalHeight - 1 || col < finalWidth) && charCount < line.length()) {
                     maze[row][col] = line.charAt(charCount);
                     charCount += 1;
                     System.out.print(maze[row][col]);

But this prints out +S+ + which is not right. 但这会打印出+ S ++,这是不对的。 I am a brand new beginner programmer and having a hard time with this, thanks for any help you can offer. 我是一个全新的初学者程序员,对此感到很辛苦,感谢您能提供的任何帮助。

I fixed it!! 我修好了它!! this is what I did: 这是我所做的:

  Scanner input = new Scanner(new File("maze0.txt"));




  char maze[][] = new char[5][7];
  input.nextLine();
     for (int row = 0; row < 5; row++) {
        String fileLine = input.nextLine();
        for (int col = 0; col < 7; col++) {
              char nextChar = fileLine.charAt(col);
              maze[row][col] = nextChar;
              System.out.print(maze[row][col]);

Convert the line to character array using .toCharArray() That will give you an array of all the characters. 使用.toCharArray()将行转换为字符数组,这将为您提供所有字符的数组。 Then just feed them into your array. 然后只需将它们输入到您的数组中即可。

it'd look something like.. 它看起来像..

// everytime we come to a new row, read a line from the file, convert it into letters then proceed on feeding them into columns.
for (int row = 0; row < finalHeight; row++) 
{
      String line = input.nextLine();
      Char[] chars = line.toCharArray();
      if(!input.hasNextLine())
            break;            // if there is no more lines to read, break the loop.
      for (int col = 0, i = 0; (col < finalWidth && i < chars.length); col++,i++) 
      {
        maze[row][col] = chars[i];
        System.out.print(maze[row][col]);
      }
}

Actually, while the screen might display +S+ +, you only have one value in your array - at maze[0][0] (a value of '+'). 实际上,尽管屏幕可能显示+ S ++,但是数组中只有一个值-迷宫[0] [0](值为“ +”)。 Your while loop reads the entire file before the for loops ever increment.For each line it reads, it sets maze[row][column] = line.charAt(charCount); 您的while循环会在for循环递增之前读取整个文件。对于读取的每一行,它将设置maze[row][column] = line.charAt(charCount); -- but row and column never get incremented because, well, there's another line to read. -但是rowcolumn永远不会增加,因为,还有另一行需要阅读。 So it reads another line and overwrites maze[0][0] to be the line.charAt(1) (because you incremented charCount). 因此,它将读取另一行,并将maze[0][0]覆盖为line.charAt(1)(因为您增加了charCount)。 This character is the space. 这个字符就是空格。 Then it loops back through because there's another line to read, and puts the 3rd character at maze[0][0] . 然后,由于还有另一行要读取,因此它循环返回,并将第三个字符放在maze[0][0] So on and so forth. 等等等等。 When it's read the entire file, then it steps through the for loops, but while (input.hasNextLine()) doesn't execute because it's already read the entire file. 当读取整个文件时,它将逐步执行for循环,但是while (input.hasNextLine())不会执行,因为它已经读取了整个文件。

you just need two loops why are you running 3 loops? 您只需要两个循环,为什么要运行3个循环?

Scanner sc = new Scanner(new File("maze.txt"));
String line = null;
for(int i = 0; i< 5;i++)
{
      line = sc.readLine()
      for(int j = 0; j < 7; j++)
      {
          maze[i][j] = line.charAt(j);
      }
}

This snippet should read the file and store it in a matrix. 该代码片段应读取文件并将其存储在矩阵中。

Since you are reading the line in the inner loop, you are printing the diagonal. 由于您正在读取内循环中的线,因此您正在打印对角线。

Here is a simple and efficient way to do it. 这是一种简单有效的方法。

public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("maze0.txt"));

    char maze[][] = new char[5][7];
    for (int i = 0; i < maze.length; i++) {
        //Get each line and convert to character array.
        maze[i] = input.nextLine().toCharArray();
    }
}

You can read each line from the file and convert it to char array. 您可以从文件中读取每一行并将其转换为char数组。

public static void main(String[] args) throws IOException {
    Scanner scan = new Scanner(new File("maze0.txt")); 
    String b;
    char maze[][] = new char[5][7];
    for (int row = 0; row < 5; row++) {
        while ( scan.hasNextLine() ){
            b = scan.nextLine();
            maze[row] = b.toCharArray();
            System.out.println(maze[row]);
        }
    }    
    scan.close();
}
import java.io.*;
import java.util.*;

public class B 
{
    public static void main(String...aaadf)throws FileNotFoundException
    {

        Scanner sc = new Scanner(new File("D://maze.txt"));
        char maze[][] = new char[5][7];
        String line = null;
        for(int i = 0; i< 5;i++)
        {
              line = sc.nextLine();
              for(int j = 0; j < 7; j++)
              {
                  maze[i][j] = line.charAt(j);
              }
        }
        for(int i = 0; i< 5;i++)
        {

              for(int j = 0; j < 7; j++)
              {
                  System.out.print(maze[i][j]);
              }
              System.out.println();
        }
    }
}

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

相关问题 如何从文本文件中读取单个字符并将它们存储到二维数组中? - How do I get individual characters from read in text file and store them into a 2D array? 如何获取Java以将文本文件存储在2D数组中并在所述数组中定位特定点? - How do I get java to store a text file in a 2D array and locate a specific point in said array? 我如何获得 Java 中二维数组的最小总和? - How do i get minimum sum of 2D Array in Java? 如何在 java 中将 a.txt 文件读入二维数组? - How do I read a .txt file into a 2D array in java? 如何将字符从文件写入2D数组? - How can I write characters from a file to a 2D array? 如何在JAVA中创建二维字符数组 - How to Create a 2D array of characters in JAVA 如何在 Java 中对二维数组进行深拷贝? - How do I do a deep copy of a 2d array in Java? JAVA如何扫描.txt文件并使用charAt(int)将单个字符分配给2d布尔数组? - JAVA How do you scan a .txt file and allocate individual characters it to a 2d boolean array using charAt(int)? 我如何获得在Java中将2D数组打印为两个文件的功能? - How do I get the function to print out a 2D array into two files in java? Java:如何从二维双精度数组中获取指定坐标的整数(x,y) - Java: How do I get the integer (x,y) of a specify coordinate from a 2d double array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM