Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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,我绊倒了。我正在为班级做一个项目,在我的一生中,我无法从一个文本文件中创建多个数组。我一直在检查,但我读到的大多数方法都不准确。我能够成功地将文件获取到数组中,但尝试通过hasNext和nextLine创建数组只会得到空数据。有什么建议吗 public static void main (String [] args) throws FileNotFoundException { int count = 0; double sum = 0; String inputf

我绊倒了。我正在为班级做一个项目,在我的一生中,我无法从一个文本文件中创建多个数组。我一直在检查,但我读到的大多数方法都不准确。我能够成功地将文件获取到数组中,但尝试通过hasNext和nextLine创建数组只会得到空数据。有什么建议吗

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) ;
    }

}    

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

  • 在读取文件之前进行文件验证

    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());
    }
    
  • 我不建议这样做,因为有时候列表的大小 数组将不接受

  • 迭代
    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++;
    }
    

  • 以上所有解释都是基于我对你问题的假设。如果我错了,很抱歉。

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

  • 在读取文件之前进行文件验证

    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());
    }
    
  • 我不建议这样做,因为有时候列表的大小 数组将不接受

  • 迭代
    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++;
    }
    

  • 以上所有解释都是基于我对你问题的假设。如果我错了,很抱歉。

    你能澄清一下你想要得到什么输出吗?这是一个行数组,其中每一行都是该行的单词数组吗?不要尝试获取一个行数组。更重要的是,我只是想把数组(文本文件)加载到编译器中,这样我就可以按照代码中的说明输出数据了。你能澄清一下你想得到什么样的输出吗?这是一个行数组,其中每一行都是该行的单词数组吗?不要尝试获取一个行数组。更重要的是,我只是尝试将数组(文本文件)加载到编译器中,以便按照代码中的说明输出数据。看起来您没有处理开销信息(第一行信息)。看起来您没有处理开销信息(第一行信息)。