在java中从文件填充二维字符数组

在java中从文件填充二维字符数组,java,arrays,multidimensional-array,char,Java,Arrays,Multidimensional Array,Char,我是java初学者,我正在尝试从输入文件填充2d字符数组。为此,我构造了一个方法,将2d字符数组作为参数变量,然后读取文件并将其存储为字符数组。到目前为止,除了填充数组之外,我已经做了所有的事情,因为当我运行代码时,程序会抛出一个NoTouchElement异常。如果有人能帮我,我将不胜感激 public static char [][] MakeWordArray (char [][] givenArray) { try { File wordFi

我是java初学者,我正在尝试从输入文件填充2d字符数组。为此,我构造了一个方法,将2d字符数组作为参数变量,然后读取文件并将其存储为字符数组。到目前为止,除了填充数组之外,我已经做了所有的事情,因为当我运行代码时,程序会抛出一个NoTouchElement异常。如果有人能帮我,我将不胜感激

public static char [][] MakeWordArray (char [][] givenArray)
    {
    try
       {
        File wordFile= new File ("words.txt");
        Scanner in= new Scanner (wordFile);
        int rows =0;
        int col=0;
        while (in.hasNextLine())
        {
            rows = rows + 1;
            col = col + 1;
            in.next();       
        }
        char [][] words = new char [rows][col];
        File wordFile2= new File ("words.txt");
        Scanner in2= new Scanner(wordFile2);
        for ( int i = 0; i < rows; i++)
        {
          for (int j = 0; j < col; j++)
          {
              String wordly = in2.nextLine();
              words [i][j] = wordly.charAt(i);
          }
        }
        return words;
       }
    catch (FileNotFoundException e)
        {
         System.out.println("File Does Not Exist");
        }
    return null;
    }
publicstaticchar[]MakeWordArray(char[]givenArray)
{
尝试
{
File wordFile=新文件(“words.txt”);
扫描仪输入=新扫描仪(wordFile);
int行=0;
int col=0;
while(在.hasNextLine()中)
{
行=行+1;
col=col+1;
in.next();
}
字符[]字=新字符[行][列];
File wordFile2=新文件(“words.txt”);
扫描器in2=新扫描器(wordFile2);
对于(int i=0;i
尝试使用do-while循环而不是while循环

do
{
rows=rows+1;
col=lol+1;
in.next();
}
while(in.hasNext());

我认为你们的计数方法有一些问题。

如果要计算.txt的行数:

int counter = 0;

         while (in.hasNextLine())
         {
            counter++;
            in.nextLine();
         }
int counterWithoutSpace = 0, counterWithSpace = 0;

         while (in.hasNextLine())
         {
            String line = in.nextLine();

            Scanner inLine = new Scanner(line);
            while (inLine.hasNext())
            {
               String nextWord = inLine.next();
               counterWithoutSpace += nextWord.length();
               counterWithSpace += nextWord.length() + 1;
            }
            counterWithSpace--;
         }
如果要计算.txt文件的字符数:

int counter = 0;

         while (in.hasNextLine())
         {
            counter++;
            in.nextLine();
         }
int counterWithoutSpace = 0, counterWithSpace = 0;

         while (in.hasNextLine())
         {
            String line = in.nextLine();

            Scanner inLine = new Scanner(line);
            while (inLine.hasNext())
            {
               String nextWord = inLine.next();
               counterWithoutSpace += nextWord.length();
               counterWithSpace += nextWord.length() + 1;
            }
            counterWithSpace--;
         }
如果你想数一数每行有多少个字符,我建议你。因为数组的大小是动态的
请注意,您也可以将上面的字符计数器逻辑与一起使用。
如下所示:

List<Integer> arr = new ArrayList<Integer>();

         while (in.hasNextLine())
         {
            arr.add(in.nextLine().length());
         }
您可以按如下方式转换其整个函数,以获得.txt的每个
字符的列表:

List<Character> arr = new ArrayList<Character>();

         while (in.hasNextLine())
         {
            String line = in.nextLine();

            for (char c : line.toCharArray())
            {
               arr.add(c);
            }
         }
List arr=new ArrayList();
while(在.hasNextLine()中)
{
String line=in.nextLine();
for(char c:line.toCharArray())
{
arr.add(c);
}
}

这里有多个问题

1) 为什么在不使用char[]]参数的情况下提供它

2) 为什么要使用两个文件,而只需从文件中读取并在二维数组中转换它

3) 方法名称应遵循驼峰式大小写约定

根据我对你问题的理解,这是我尝试过的代码

注意-由于要求的是
数组
而不是动态数据类型,如
列表数组列表
等,因此输入字符数组的数据可能会丢失

说这就是有效的方法

  public class StackOverflow{
    public static char [][] makeWordArray ()
    {
        try{
            File f  = new File("C:\\docs\\mytextfile.txt");
            Scanner scan = new Scanner(f);
            int row = 0, col = 0;
            String readData = "";
            while(scan.hasNextLine()){
                readData += scan.nextLine(); 
                row++;  
            }
            double range = (readData.length()/row); 
            col = (int)Math.ceil(range);
            char[][] arr = new char[row][col];
            int count = 0;
            for (int i = 0; i < row; i++){
                for(int j = 0; j < col; j++){
                    arr[i][j] = readData.charAt(count++); 
                    System.out.println("Pos: ["+ i +"][" + j + "]" + arr[i][j]);
                }
            }
            return arr;
        }
        catch(FileNotFoundException fe){
            System.err.println(fe.getMessage());
            return null;
        }
    }
    public static void main(String[] arg){
        char[][] myarr = StackOverflow.makeWordArray();
        //print your array
    }
 }
公共类堆栈溢出{
公共静态字符[][]makeWordArray()
{
试一试{
文件f=新文件(“C:\\docs\\mytextfile.txt”);
扫描仪扫描=新扫描仪(f);
int行=0,列=0;
字符串readData=“”;
while(scan.hasNextLine()){
readData+=scan.nextLine();
行++;
}
双范围=(readData.length()/行);
col=(int)Math.ceil(range);
char[][]arr=新字符[行][col];
整数计数=0;
对于(int i=0;i
您必须在.hasNextLine中使用吗?尝试使用in.hasNext