Java 读取文本文件中的下一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19595368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:37:52  来源:igfitidea点击:

Reading the next line in a text file

javaloopstextinput

提问by coinbird

I'm trying to read input from a text file that will be formatted like this:

我正在尝试从文本文件中读取输入,该文件的格式如下:

2 80 97 
5 69 79 89 99 58 
7 60 70 80 90 100 0 59

The first number of each line is the number of "grades" per "section."

每行的第一个数字是每个“部分”的“等级”数。

I got my program to read one section, but I can't figure out how to make it read how many sections there will be, and then read the next line(s).

我让我的程序读取一个部分,但我不知道如何让它读取将有多少个部分,然后读取下一行。

I think I can put my current code in a count controlled loop that will first read how many sections there are, and run the loop that many times. I just don't know how to convert that idea to code.

我想我可以将我当前的代码放在一个计数控制循环中,该循环将首先读取有多少个部分,然后多次运行循环。我只是不知道如何将这个想法转换为代码。

Here is the revevant code section:

这是相关的代码部分:

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

  //int sections = (in.nextInt());
  int scores = (in.nextInt());
  int scoresForAverage = scores;
  int scoreTotals = 0;
  double average = 0;
  int A = 0;
  int B = 0;
  int C = 0;
  int D = 0;
  int F = 0;

  int highest = 0;
  int lowest = 100;
  while (scores > 0 && in.hasNextInt())
  {
     int grade = in.nextInt();
     if (grade >= 90)
        A++;
     else if (grade >= 80)
        B++;
     else if (grade >= 70)
        C++;
     else if (grade >= 60)
        D++;
     else
        F++;

     if (grade > highest) 
        highest = grade;
     if (grade < lowest)
        lowest = grade;

     scores--;
     scoreTotals = (scoreTotals + grade);
   }  

  average = scoreTotals/scoresForAverage;

  System.out.println("Scores for section 1");
  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: " + lowest);
  System.out.println("Highest score: " + highest);
  System.out.println("Average: " + average);

EDIT: Updated with complete method.

编辑:用完整的方法更新。

采纳答案by Embattled Swag

Since you know the first int in each line ISNT a grade, you can save each line using someString = in.nextLine()while in.hasNextLine()and then iterate over each saved string skipping the first integer using a new Scannerinstance for each line.

由于您知道每行中的第一个 int 不是一个等级,因此您可以使用someString = in.nextLine()while保存每一行in.hasNextLine(),然后使用Scanner每行的新实例遍历每个保存的字符串,跳过第一个整数。

回答by Arash Saidi

If you are using Scanner, you can use the method hasNext();

如果您使用的是 Scanner,则可以使用 hasNext() 方法;

This will be true as long as there are any strings in the text separated by a whitespace.

只要文本中有任何由空格分隔的字符串,就会出现这种情况。

回答by nhgrif

You can read the entire line, then use the String.splitmethod to split this into an array using a space delimiter.

您可以读取整行,然后使用该String.split方法使用空格分隔符将其拆分为一个数组。

After you read the line:

在你阅读这行之后:

String grades[] = line.split(" ");

Then you can use a forloop as such...

然后你可以使用一个for循环......

for(int i=1; i<grades.length; ++i) { 
//start an index 1 to skip the non-grade first number on line
    int grade = parseInt(grades[i]);
    if (grade >= 90)
        A++;
    //etc on down the line
}

And contain this entire process in a whileloop to iterate over every line.

并将整个过程包含在while循环中以迭代每一行。