文本文件中的多个数组

时间:2017-06-08 08:49:38

标签: java arrays

我绊倒了。我正在为一个项目而工作,对于我的生活,我无法想象从文本文件创建多个数组。我一直在检查,但我读过的大部分方法都有不确定性。我能够成功地将文件获取到一个数组中,但是尝试通过hasNext创建一个数组,而nextLine只给我一个空数据。有什么指针吗?

<DataGridTextColumn Header="Rate" Width ="*" Binding="{local:NullableBinding rate}"/>

文本文件如下所示。

public static void main (String [] args) throws FileNotFoundException {  
    int count = 0;
    double sum = 0;
    String inputfile = "input.txt";
    File filename = new File (inputfile);
    Scanner console = new Scanner(filename);   
    ArrayList<String> list = new ArrayList<String>();
    while (console.hasNext()) {
        list.add(console.nextLine());
    }

    Scanner user = new Scanner (System.in);
    System.out.println("Please verify file before moving forward.");
    String fileverify = user.nextLine();
    String line = "";

    if (fileverify.equals("input.txt")) {
        System.out.println("File is available.\n");
    }
    else {
        System.out.println("Wrong file. Please try again.");
        return;
    }
    System.out.println(list);

    String[] studentNames = new String [count];
    int[]studentIDs = new int[count];
    double[]studentScores = new double[count];
    count++;

    while(console.hasNext()){

        studentNames[count] = console.nextLine();
        studentIDs[count] = console.nextInt();
        studentScores[count] = console.nextDouble();

    }

    System.out.println("Would you like to:\n" +
                        "\n1. Print average" +
                        "\n2. Print high score"+
                        "\n3. Print low score"+
                        "\n4. Print median"+
                        "\n5. Print all student names and scores"+
                        "\n6. Exit\n");
    String command = user.next(); 

    int nextInt = console.nextInt();
    if (user.equals("1")){
          count++;
          sum = sum + console.nextInt();
          System.out.println("The average is : " + sum / count) ;
    }

}    

1 个答案:

答案 0 :(得分:0)

我希望您需要实现的是读取文件,然后将每行添加到ArrayList String类型,并将每个ArrayList元素拆分为三个不同的数组并操作数据基于这些数组,如果是这样的话

  1. 在阅读文件之前进行文件验证。

    String inputfile = "input.txt";
    File filename = new File (inputfile);
    
    Scanner user = new Scanner (System.in);
    System.out.println("Please verify file before moving forward.");
    String fileverify = user.nextLine();
    
    if (fileverify.equals("input.txt")) {
        System.out.println("File is available.\n");
    }
    else {
        System.out.println("Wrong file. Please try again.");
        return;
    }
    
    Scanner console = new Scanner(filename);   
    ArrayList<String> list = new ArrayList<String>();
    while (console.hasNext()) {
        list.add(console.nextLine());
    }
    
  2. 声明与列表大小相同的数组

    String[] studentNames = new String [list.size()];
    int[] studentIDs = new int[list.size()];
    double[] studentScores = new double[list.size()];
    
  3.   

    我不推荐这个,因为有时候列表的大小可以   阵列不会接受。

    1. 迭代ArrayList将元素添加到数组

      for(String line : list) {
      
         String[] lineSplitArray = line.split(":");
         studentNames[count] = lineSplitArray[0];
         studentIDs[count] = Integer.parseInt(lineSplitArray[1]);
         studentScores[count] = Double.parseDouble(lineSplitArray[2]);
         count++;
      }
      
    2. 以上所有解释均基于我对您的问题的假设。对不起,如果我错了。