我如何得到一个文件的字符到Ja​​va中的二维数组? [英] How do i get characters in a file into a 2D array in Java?

查看:164
本文介绍了我如何得到一个文件的字符到Ja​​va中的二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个看起来像这样的文件:

  +  -  +  -  +  -  +(/ N)
| S | | (/ N)
+ + + +(/ N)
| | E | (/ N)
+ - + - + - +(/ N)

/ N 是文件中的新行

和我想在这里有每个字符作为一个5×7阵列的条目。我不知道该怎么做,这是我曾尝试(有很多其他的事情一起输入的文件):

 公共静态无效的主要(字串[] args)抛出FileNotFoundException异常{  扫描仪输入=新的扫描仪(新的文件(maze0.txt));  炭迷宫[] [] =新的char [5] [7];
  INT charCount = 0;
     对于(INT行= 0;&行LT; finalHeight;排++){
        对于(INT COL = 0;&山坳下,finalWidth;西++){
           而(input.hasNextLine()){
               串行= input.nextLine();
              如果((行< finalHeight - 1 ||山坳< finalWidth)及和放大器; charCount< line.length()){
                     迷宫[行] [山口] = line.charAt(charCount);
                     charCount + = 1;
                     System.out.print(迷宫[行] [COL]);

但这种打印出+ S + +这是不正确的。我是一个全新的初学编程的,并有一个很难与此,感谢您可以提供任何帮助。

我固定它!这是我做过什么:

 扫描仪输入=新的扫描仪(新文件(maze0.txt));
  炭迷宫[] [] =新的char [5] [7];
  input.nextLine();
     对于(INT行= 0;排小于5;排++){
        串fileLine = input.nextLine();
        对于(INT COL = 0;山坳7;;西++){
              焦炭nextChar = fileLine.charAt(COL);
              迷宫[行] [山口] = nextChar;
              System.out.print(迷宫[行] [COL]);


解决方案

 进口java.io. *;
进口的java.util。*;大众B级
{
    公共静态无效的主要(字符串... aaadf)抛出FileNotFoundException异常
    {        扫描仪SC =新的扫描仪(新File(D://maze.txt));
        炭迷宫[] [] =新的char [5] [7];
        串线= NULL;
        的for(int i = 0;我小于5;我++)
        {
              行= sc.nextLine();
              为(中间体J = 0; J&下; 7; J ++)
              {
                  迷宫[I] [J] = line.charAt(J);
              }
        }
        的for(int i = 0;我小于5;我++)
        {              为(中间体J = 0; J&下; 7; J ++)
              {
                  System.out.print(迷宫[I] [J]);
              }
              的System.out.println();
        }
    }
}

So i have a file that looks like this:

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

/n being a new line in the file

and i want to have each character here as an entry in a 5x7 array. 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. 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]);

解决方案

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

这篇关于我如何得到一个文件的字符到Ja​​va中的二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆