如何将.dat文件导入多个数组

时间:2016-09-12 06:04:27

标签: java

好吧,我正在研究一个读取周期表的程序,你可以根据数字或缩写搜索元素。

无论如何,我试图将周期表文件读入4个不同的数组有点困难:原子序数,缩写,元素名称和原子量。

我不知道如何编写单个方法将所有信息一次性导入每个数组。我希望有一个包含所有这些数组的类,并且我可以在以后需要每个数组时调用它。

这是我到目前为止所得到的,顺便说一句,我有点生疏......我想在这个程序上工作会让我熟悉基础知识。

class PeriodicTable{
    private String fileName = "periodictable.dat";

    private int[]    atomicNumTable    = new int[200];
    private String[] abbreviationTable = new String[200];
    private String[] nameTable         = new String[200];
    private double[] atomicWeightTable = new double[200];

    PeriodicTable(String fileName){
        readTable(fileName);
    }

    public int[] readTable(String fileName){
        Scanner inFile = null;
        try{
            inFile = new Scanner(new File(fileName));
        }catch(FileNotFoundException nf){
            System.out.println(fileName + " not found");
            System.exit(0);
        }

        atomicNumTable = new int[200];
        int i = 0;
        while(inFile.hasNext() && i < atomicNumTable.length){
            int number = inFile.nextInt();
            atomicNumTable[i] = number;
            i++;
        }
        inFile.close();
        return atomicNumTable;
    }
}

以下是表格的每一行:

1    H   Hydrogen    1.00794

3 个答案:

答案 0 :(得分:2)

  1. 只需使用java.lang.String.split(' ')(假设您的列使用空格分隔;如果使用其他内容;您只需要调整该正则表达式参数!)
  2. 那将返回一个字符串数组;你基本上现在:第一列应该是一个int,然后你得到两个字符串,然后是一个double值。或者让我们准确一点:你得到字符串,意味着别的东西;因此你必须研究像Integer.valueOf()这样的方法;和Double相似。
  3. 从那里开始工作不应该太难。

    但我建议对你的逻辑进行一些修改: 4 不同的表根本没有意义。良好的OO编程是关于创建有用的抽象。如果没有抽象,您的程序将变为抽象本身。

    含义:你应该引入类似

    的类
    public class Element {
      private final int id;
      private final String abbreviation;
      private final String fullName;
      private final double atomicWeight;
    
      ... with one constructor that takes all 4 parameters
    
      ... with getter methods for the fields of this class
    
      ... and meaningful overrides for equals() and hashcode()
    

    }

    然后,而不是创建 4 数组;你创建一个数组,甚至更好ArrayList<Element>。而不是将4个值推送到4个不同的数组中,而是在每个循环迭代中创建一个新的Element对象;然后将该新对象添加到列表中。

    您解决方案的主要区别在于:您可以整体处理元素;而在您的解决方案中,单个“元素”基本上是指向4个不同表的索引。

答案 1 :(得分:2)

您可以大量简化此代码。尝试这样的事情。

1)逐行读取文件,随意分割线条, 将值添加到包含String []

的某个ArrayList

2)关闭你的文件

3)将ArrayList转换为String [] []

4)打印结果

另请注意,Java中的数组从0开始索引,而不是从1开始。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;

public class Test {

    static public void main(String[] args) throws Exception {

        File file = new File("periodictable.dat");
        FileReader reader = new FileReader(file);
        BufferedReader buffReader = new BufferedReader(reader);
        String s = null;

        ArrayList<String[]> lst = new ArrayList<String[]>();
        String[][] res = null;

        while((s = buffReader.readLine()) != null){
            String[] arr = s.split("[\\s]+");
            lst.add(arr);
        }

        buffReader.close();

        res = new String[lst.size()][lst.get(0).length];
        res = lst.toArray(res);

        System.out.println();

        // System.out.println(res);

        // String result = Arrays.deepToString(res);
        // System.out.println(result);
        System.out.println();

        for (int i=0; i<res.length; i++){
            for (int j=0; j<res[i].length; j++){
                System.out.println("res[" + (i+1) + "][" + (j+1) + "]=" + res[i][j]);
            }
        }

        System.out.println();
    }

}

<强>输出:

res[1][1]=1
res[1][2]=H
res[1][3]=Hydrogen
res[1][4]=1.00794

值迭代每行的索引

答案 2 :(得分:0)

您可以在循环中区分四种情况:

  • i%4 == 0

  • i%4 == 1

  • i%4 == 2

  • i%4 == 3

根据这一点,您知道您必须阅读的下一个值的类型。因此,您可以搜索整数,字符串或浮点数,并将值放在正确的位置。

我支持GhostCat的建议只有一个数组和一个包含一行的四个值而不是四个数组的类。

相关问题