Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.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 如何将文件中的数据读取到二维数组的索引中?_Java_Arrays - Fatal编程技术网

Java 如何将文件中的数据读取到二维数组的索引中?

Java 如何将文件中的数据读取到二维数组的索引中?,java,arrays,Java,Arrays,我的数据如下所示: 1 2 3 1 3 7 2 1 6 4 3 8 我希望得到一个数组c[I][j],数据的第一列是[I],第二列是[j],值进入数组 我是java新手,我读过一些关于文件输入的书,比如: import java.io.*; public class Words { public static void main(String[] args) throws FileNotFoundException, IOException { proces

我的数据如下所示:

1 2 3
1 3 7
2 1 6
4 3 8
我希望得到一个数组c[I][j],数据的第一列是[I],第二列是[j],值进入数组

我是java新手,我读过一些关于文件输入的书,比如:

import java.io.*;
public class Words {
    public static void main(String[] args)
       throws FileNotFoundException, IOException {
       processFile("words.txt");
    }

    public static void processFile(String filename)
      throws FileNotFoundException, IOException {
      FileReader fileReader = new FileReader(filename);
      BufferedReader in = new BufferedReader(fileReader);
      while (true) {
          String s = in.readLine();
          if (s == null) break;
            System.out.println(s);
          }
      }
    }
他们是将代码更改为我想要的代码的好方法吗


谢谢你的帮助

这应该能奏效。基本上是一样的,但我还添加了解析方法。您可以使用split解析每一行,然后通过valueOf方法轻松地将其转换为整数

public static void main(String[] args)
        throws FileNotFoundException, IOException {
    processFile("words.txt");
}

public static void processFile(String filename) throws FileNotFoundException, IOException {
    int[][] array = new int[4][3];
    int lineCount = 0;
    try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
        String line = br.readLine();
        while (line != null) {
            String[] parts = line.split(" ");
            for (int i = 0; i < parts.length; i++) {
                array[lineCount][i] = Integer.valueOf(parts[i]);
            }
            lineCount++;
            line = br.readLine();
        }
    }
}

如果输入保证为3个数字整数,而不是浮点,由2个空格分隔,则可以使用此选项

如果输入是可变的或不可信的,那么您可以在解析之前添加更多检查

private static ArrayList<Integer[]> list;

public static void main(String[] args) throws FileNotFoundException, IOException
{
    processFile("words.txt");
}

public static void processFile(String filename) throws FileNotFoundException, IOException, NumberFormatException
{
    BufferedReader in = new BufferedReader(new FileReader(filename));

    String line = null;

    list = new ArrayList<Integer[]>();

    while((line = in.readLine()) != null)
    {
        String[] split = line.split("\\s{1}");

        list.add(
            new Integer[]
                {
                    Integer.parseInt(split[0]), 
                    Integer.parseInt(split[1]), 
                    Integer.parseInt(split[2]),
                });
    }
}
不过,从技术上讲,您应该这样做:

private static ArrayList<Integer[]> list;

public static void main(String[] args) throws FileNotFoundException, IOException
{
    processFile("words.txt");
}

public static void processFile(String filename) throws FileNotFoundException, IOException, NumberFormatException
{
    BufferedReader in = null;

    String line = null;

    list = new ArrayList<Integer[]>();

    try
    {
        in = new BufferedReader(new FileReader(filename));

        while((line = in.readLine()) != null)
        {
            String[] split = line.split("\\s{1}");

            list.add(
                new Integer[]
                    {
                        Integer.parseInt(split[0]), 
                        Integer.parseInt(split[1]), 
                        Integer.parseInt(split[2]),
                    });
        }
    }
    catch(FileNotFoundException e)
    {
        e.printStackTrace();
        throw e;
    }
    catch(IOException e)
    {
        e.printStackTrace();
        throw e;
    }
    catch(NumberFormatException e)
    {
        e.printStackTrace();
        throw e;
    }
    catch(Exception e)
    {
        e.printStackTrace();
        throw e;
    }
    finally
    {
        if(in != null)
        {
            in.close();
        }
    }
}

我不知道我是否明白你到底想用2D阵列做什么。你能说得更具体些吗?也许更多的数据输入和输出会有所帮助。你是在问如何解析数据吗?如果是这样的话,你希望在c[i][j]数组中包含12317372160438的哪一部分?哦,数据看起来不对。它应该像123,137,2116…我应该用不同的行…一行有三个数字,没有逗号。谢谢,我想这会有帮助的!