Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从字符串填充二维数组,并将单个字符的ASCII值放入其中_Java - Fatal编程技术网

Java 从字符串填充二维数组,并将单个字符的ASCII值放入其中

Java 从字符串填充二维数组,并将单个字符的ASCII值放入其中,java,Java,我正在创建一个程序,它将读取一个txt文件(来自另一个主txt文件),获取一个字符串,它是一组字母(@-K),然后用字符串中字符的ASCII值填充一个2d数组。因此,我已经成功地创建了2d数组并读取了该文件。问题是我的数组只由字符串文件的第一个字母填充,而不是遍历整个文件。我有一个if语句,我认为应该通读该文件并正确填充它,但是它没有这样做。不知道我做错了什么。这是代码 public static void main(String[] args) throws IOException

我正在创建一个程序,它将读取一个txt文件(来自另一个主txt文件),获取一个字符串,它是一组字母(@-K),然后用字符串中字符的ASCII值填充一个2d数组。因此,我已经成功地创建了2d数组并读取了该文件。问题是我的数组只由字符串文件的第一个字母填充,而不是遍历整个文件。我有一个if语句,我认为应该通读该文件并正确填充它,但是它没有这样做。不知道我做错了什么。这是代码

     public static void main(String[] args) throws IOException 
  {
    String txtfile;
    String txtfilecontents;
    int matrix[][]= new int [24][34];
    int row=0;
    int col=0;
    BufferedReader masterfile = new BufferedReader(new FileReader("imageFileList.txt")); 
    while((txtfile=masterfile.readLine())!=null)                                              //reads master text file
    {
      BufferedReader imagefile= new BufferedReader(new FileReader(txtfile));
      while((txtfilecontents=imagefile.readLine())!=null)                                     //reads text file within master file
      {
        for(int i=0;i<txtfilecontents.length();i++)                                           
        {
          if(col ==34)
          {
            col = 0;
            row++;
          }
          if(row ==24)
          {
            col=0;
            row=0;
            break;
          }
          matrix[row][col] = (int)txtfilecontents.charAt(i)-64;                                  //instead of adding every character it only adds the first one until it fills up the 2d array
          col++;
        }
        System.out.println(txtfilecontents);
        for(row=0;row<24;row++)
        {
          for(col=0;col<34;col++)
            System.out.printf("%4d",matrix[row][col]);
          System.out.println();
        }
      }

      System.out.println(txtfile);
    }


  }   
此外,我还在文件中添加了一组print语句,以便查看输出结果,但稍后会将其取出。

for(int I=0;I循环

    for(int i=0;i<txtfilecontents.length();i++)                                           
        {
          for(row=0;row<24;row++)
            for(col=0;col<34;col++)
              matrix[row][col] = (int)txtfilecontents.charAt(i);                              //instead of adding every character it only adds the first one until it fills up the 2d array
        }
for(int i=0;i<txtfilecontents.length();i++)                                           
        {
          for(row=0;row<24;row++)
            for(col=0;col<34;col++)
              matrix[row][col] = (int)txtfilecontents.charAt(i);                              //instead of adding every character it only adds the first one until it fills up the 2d array
        }

给定示例输入,预期的输出应该是什么?此外,减去64不会给出任何东西的ASCII值…示例输出应该是用输入的ASCII值填充的2d数组。减去64只是为了得到0-11个值(@ASCII值为64,减去64=0,等等)那么,您的输出应该是ASCII值还是ASCII值减64…?最终结果将是ASCII值减64。现在我只是想让输出实际正确。我的打印语句显示这正是它所做的。我尝试了Apraoch@Floris建议的方法,但它只填充了第一个数字,然后给了我一个数字一群0。@Student为我的答案添加了一个可能的解决方案谢谢你,我使用了非常类似的方法来修复它。但是,现在我遇到了一个错误,它将不会读取下一个txt文件。我更新了主要问题中的代码。我在代码中省略了一行…不是递增的
。它现在应该可以工作了。
row = 0; 
col = 0;
for(int i=0;i<txtfilecontents.length();i++)                                           
{
    //Insert the char into the matrix
    matrix[row][col] = (int)txtfilecontents.charAt(i);

    //Move the matrix position for the next character
    row++;
    if (row >= 24){
        row = 0;
        col++;
        if (col >= 34){
            System.out.println("Matrix out of space");
        }
    }
}
for(int i=0;i<txtfilecontents.length();i++)                                           
        {
          for(row=0;row<24;row++)
            for(col=0;col<34;col++)
              matrix[row][col] = (int)txtfilecontents.charAt(i);                              //instead of adding every character it only adds the first one until it fills up the 2d array
        }
row=0;col=0;
for(int i=0;i<txtfilecontents.length();i++)                                           
    {
         if (col == 34) {
            col=0; row++;
         }
    if (row==24) break;
    matrix[row][col] = (int)txtfilecontents.charAt(i);        
    col++; // <<<<< added this line!!                      
    }