Java 使用BufferedReader读取行

Java 使用BufferedReader读取行,java,Java,我试图从文件中读取信息来解释它。我想读每一行输入。我最近刚刚了解了bufferedreader。然而,我的代码的问题是它每隔一行就跳过一行 例如,当我输入8行数据时,它只打印其中4行。偶数的 代码如下: import java.io.*; import java.util.Scanner; public class ExamAnalysis { public static void main(String[] args) throws FileNotFoundException, IOEx

我试图从文件中读取信息来解释它。我想读每一行输入。我最近刚刚了解了bufferedreader。然而,我的代码的问题是它每隔一行就跳过一行


例如,当我输入8行数据时,它只打印其中4行。偶数的

代码如下:

import java.io.*;
import java.util.Scanner;

public class ExamAnalysis
{
  public static void main(String[] args) throws FileNotFoundException, IOException
  {

    int numOfQ = 10;

    System.out.println();
    System.out.println("Welcome to Exam Analysis.  Let’s begin ...");
    System.out.println();
    System.out.println();

    System.out.println("Please type the correct answers to the exam     questions,");
    System.out.print("one right after the other: ");
    Scanner scan = new Scanner(System.in);
    String answers = scan.nextLine();

    System.out.println("What is the name of the file containing each student's");
    System.out.print("responses to the " + numOfQ + " questions? ");
    String f = scan.nextLine();
    System.out.println();

    BufferedReader in = new BufferedReader(new FileReader(new File(f)));
    int numOfStudent= 0;

    while ( in.readLine() != null )
    {
      numOfStudent++;
      System.out.println("Student #" + numOfStudent+ "\'s responses: " + in.readLine());
    }
    System.out.println("We have reached “end of file!”");             
    System.out.println();
    System.out.println("Thank you for the data on " + numOfStudent+ " students. Here is the analysis:");
   }
 }
 }
我知道这可能是一个有点糟糕的写作风格。我只是对编码非常陌生。所以,如果有任何方法可以帮助我修正代码和方法的风格,我会非常激动

该程序的目的是将答案与正确答案进行比较。 因此,我还有另一个问题:

如何将字符串与缓冲读取器进行比较? 比如,我如何将ABCCED与ABBBDE进行比较,以确定前两个匹配,而其余的不匹配


谢谢

您没有将字符串与
readLine()
进行比较。将它们与
String.equals()进行比较。

由于中提到的原因,您的阅读代码会跳过每一行

我的代码的问题是它每隔一行就跳过一行

您的EOF检查在每次迭代中都会有一行

while ( in.readLine() != null ) // read (first) line and ignore it
{
  numOfStudent++;
  System.out.println("Student #" + numOfStudent+ "\'s responses: " + 
    in.readLine());   // read (second) next line and print it
}
要读取所有行,请执行以下操作:

String line = null;
while ( null != (line = in.readLine())) // read line and save it, also check for EOF
{
  numOfStudent++;
  System.out.println("Student #" + numOfStudent+ "\'s responses: " + 
    line);   // print it
}

要比较字符串,需要使用该方法。如果返回值为
0

,则两个字符串相等。当我输入8行数据时,它只打印其中4行
readLine()
@萨蒂娅,不,这不是我的问题。你能帮忙吗?嗨,谢谢你的回答。我试过读那个文件,但对我来说没有意义。如果我要求太多,我很抱歉,但是你能告诉我如何解决这个问题吗。我正在努力学习,非常感谢您的帮助。谢谢。:)