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

时间:2014-03-03 01:12:01

标签: 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 {
       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);
          }
      }
    }

他们是将代码更改为我想要的好方法吗?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

这应该可以解决问题。它基本相同,但我还添加了解析方法。您可以使用“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();
        }
    }
}

答案 1 :(得分:0)

如果输入保证为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();
        }
    }
}