Wilker Iceri
Wilker Iceri

Reputation: 141

Java BufferedReader readLine method don't return nothing

I have a problem to resolve: http://www.urionlinejudge.com.br/judge/problems/view/1117

With my code, when code reach the last line the method readLine don't return nothing. It waits i type other line of input.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ValidacaoDeNota {
   public static void main(String[] args) throws IOException {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

      String line = null;
      double num;

      Double ultimaNotaValida = null;

      while ((line=br.readLine()) != null) {
         num = Double.parseDouble(line);

         if (num < 0 || num > 10) {
            bw.write("nota invalida");
         } else if (ultimaNotaValida == null) {
            ultimaNotaValida = num;
         } else {
            bw.write("media = " + ((ultimaNotaValida+num) / 2) );
         }

         bw.write("\n");
      }

      bw.flush();
   }
}

Upvotes: 1

Views: 1578

Answers (1)

user207421
user207421

Reputation: 310884

You need to enter Ctrl/d (Windows) or Ctrl/z (Unix, Linux etc) to produce an end of stream at the console. Then readLine() will return null and your loop will terminate.

Upvotes: 1

Related Questions