簡體   English   中英

如何將文件中的字符轉換為Java中的2D數組?

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

所以我有一個看起來像這樣的文件:

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

/n是文件中的新行

我想在這里將每個字符作為5x7數組中的條目。 我不知道該怎么做,這是我嘗試過的方法(還有很多其他事情。輸入是文件):

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]);

但這會打印出+ S ++,這是不對的。 我是一個全新的初學者程序員,對此感到很辛苦,感謝您能提供的任何幫助。

我修好了它!! 這是我所做的:

  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]);

使用.toCharArray()將行轉換為字符數組,這將為您提供所有字符的數組。 然后只需將它們輸入到您的數組中即可。

它看起來像..

// 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]);
      }
}

實際上,盡管屏幕可能顯示+ S ++,但是數組中只有一個值-迷宮[0] [0](值為“ +”)。 您的while循環會在for循環遞增之前讀取整個文件。對於讀取的每一行,它將設置maze[row][column] = line.charAt(charCount); -但是rowcolumn永遠不會增加,因為,還有另一行需要閱讀。 因此,它將讀取另一行,並將maze[0][0]覆蓋為line.charAt(1)(因為您增加了charCount)。 這個字符就是空格。 然后,由於還有另一行要讀取,因此它循環返回,並將第三個字符放在maze[0][0] 等等等等。 當讀取整個文件時,它將逐步執行for循環,但是while (input.hasNextLine())不會執行,因為它已經讀取了整個文件。

您只需要兩個循環,為什么要運行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);
      }
}

該代碼片段應讀取文件並將其存儲在矩陣中。

由於您正在讀取內循環中的線,因此您正在打印對角線。

這是一種簡單有效的方法。

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

您可以從文件中讀取每一行並將其轉換為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM