java - 从文本文件读取并打印

标签 java text-files bluej sections

好吧,所以我应该使用代码从文本文件中读取和打印,但每次运行时,我都会在第 31 行“grade = in.nextInt();”上收到“java.utilNoSuchElementException”。当前文本文件是 2 80 97 5 69 79 89 99 58 7 60 70 80 90 100 0 59

其中第一个数字是每个部分的分数数量,每个部分都应该被计算在内(即1、2、3)无论如何,一次一个问题。这是当前的代码。

导入java.util.Scanner; 导入java.io.*;

公共(public)课Prog2 {

public static void main (String args []) throws IOException
{
    Scanner in = new Scanner (new File ("test1.txt"));

    int Lowest, Highest, grade = 0;
    float section_average, class_average;
    int count = 0, A = 0, B = 0, C = 0, D = 0, F = 0, total_number_of_sections = 0, total_number_of_scores = 0, number = 0;
    while (in.hasNextInt())
    {
        number = in.nextInt();
        System.out.println (in.nextInt());
        number ++;

        while (count < number)
        {
            grade = in.nextInt();
            total_number_of_sections += number;
            total_number_of_scores += grade;
            total_number_of_scores ++;

            count++;


        }

    }
    if (number > 0)
    {
        System.out.println ("Scores for section "+count);
    }
    else
    {
        System.out.println ("Scores for section 0");
    }
    if (grade >= 90)
    {
        A ++;

    }
    if (grade >= 80 && grade < 90)
    {
        B ++;

    }
    if (grade >= 70 && grade < 80)
    {
        C ++;

    }
    if (grade >= 60 && grade < 70)
    {
        D ++;

    }
    if (grade < 60)
    {
        F ++;

    }
    System.out.println (" ");
    System.out.println ("Scores for section "+count);
    System.out.println ("A's" + A);
    System.out.println ("B's" + B);
    System.out.println ("C's" + C);
    System.out.println ("D's" + D);
    System.out.println ("F's" + F);
    System.out.println ("Lowest Score: ");
    System.out.println ("Highest Score: ");
    System.out.println (" ");
    System.out.println (" ");
    System.out.println ("Total number of sections: " + total_number_of_sections);
    System.out.println ("Total number of scores: " + total_number_of_scores);
    System.out.println ("Class Average: ");



}

}

最佳答案

试试这个

while (in.hasNextInt())
{
    count = 0;
    number = in.nextInt();
    System.out.println (in.nextInt());


    while (in.hasNextInt())
    {
        grade = in.nextInt();
        total_number_of_sections += number;
        total_number_of_scores += grade;
        total_number_of_scores ++;

        if(++count == number ){ break;}


    }

}

关于java - 从文本文件读取并打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19331212/

相关文章:

java - 如何在 Spring 框架中使用 Apache 的 MultiKeyMap

ios - SWIFT TableView 错误

c# - 读取文本文件后重写它

java - 向类添加 GUI

java - 无法在 Java 中打开文本文件

java - Java无法访问包外资源

java - 在 jar 文件中加载属性文件

java - 如何在另一个类中使用在 class1 中创建的变量?

java - 使用java编辑svg或psd

mysql - 导入 .txt 到 mysql