3

I got the proper EOF criteria in Java with this question and it is doing fine. But the problem occurred when a program required an input of a blank line after each input case. The following code works fine for EOF.

    String line;
    BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
    try {
        while((line = read.readLine()) != null){
            n = Integer.parseInt(line);
        }
    }
     catch (IOException e) {} 

But the problem is, I got to solve a problem where after input of each case, a blank new line is inputted. As a result I am getting a NumberFormatException and this is expected too. I have tried all that I could do including try-catch() mechanism.

It would be great if I have a code that doesn't terminate or throw exception on a blank line input.

Community
  • 1
  • 1
Tahmid Ali
  • 805
  • 9
  • 29

4 Answers4

1

You can check whether

"".equals(line.trim())

before trying to convert it to an integer.

But an even better way would be to use a Scanner, and then use Scanner.nextInt() to get the next token. That will deal with the blanks automatically.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
1

Probably the best way to do this would be to just check the length of the input before doing anything. So:

if(line.length() > 0) {
    //do whatever you want
}
Alex K
  • 8,269
  • 9
  • 39
  • 57
  • It's working but still, if with blank spaces and then pressing enter, it keeps giving the NumberFormatException. So, the problem is still not solved. Basically I wanted a java code that supports EOF in online program submissions. My previous code works fine but it doesn't support EOF. To clarify, I am working on http://uva.onlinejudge.org/external/110/11060.html which requires that blank line after each test case. .. – Tahmid Ali Nov 29 '14 at 15:03
  • And here is my final code with the modification that you suggested. http://pastebin.com/AmqFg9gY @Alex K – Tahmid Ali Nov 29 '14 at 15:25
0

try this.

String line;
    BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
    try {
        while((line = read.readLine()) != null){
          line.replaceAll(" ","");
               if(line.length()>0){
            n = Integer.parseInt(line);
                     }
        }
    }
     catch (IOException e) {} 
Altmish-E-Azam
  • 1,561
  • 1
  • 13
  • 24
  • I have added the line `line.replaceAll(" ", "");` But it isn't working. If I put a space and press enter, I am getting the same `NumberFormatException` .. – Tahmid Ali Nov 28 '14 at 19:24
  • @TahmidNips can you clarify in brief? – Altmish-E-Azam Nov 29 '14 at 06:03
  • To be more specific, I wanted a Java code that supports EOF in a proper way. This http://pastebin.com/AmqFg9gY code is working for this http://uva.onlinejudge.org/external/110/11060.html problem. But if I input a white space and then press enter it throws NumberFormatException. So I need a Java template that won't provide any sort of exception while doing EOF. @Miya G – Tahmid Ali Nov 29 '14 at 15:23
-1

You could use a try-catch block inside the while loop, and if you catch an exception just keep going to the next iteration. It is not the best solution but for you case it should work.

String line;
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
try {
    while((line = read.readLine()) != null) {
        try {
            n = Integer.parseInt(line);
            //other stuff with n
        } catch (NumberFormatException e) {
              continue; // do nothing, and move on to the next line
        }
    }
}
 catch (IOException e) {} 
konomith
  • 24
  • 1
  • 3