53920

BufferReader reading empty lines

I am entering lines from input (IDE eclipse Luna) on pressing enter the cursor keeps moving down but no output shows up. In tried second method but i have to press enter twice to print output how can i fix both errors . Why in second method as soon as it detects blank line it doesn't print why i have to press enter twice And what commands goes when we press enter in console

<strong>FIRST METHOD</strong>

private static String para = null; public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(System.in)); String line; while ((line = bufferedReader.readLine()) != null) { para = para + line; } bufferedReader.close(); System.out.println(para); }

<strong>SECOND METHOD</strong>

String line; while (!(line = br.readLine()).equals("")) { s1 = line.split(" "); for (int j = 1; j < s1.length; j++) { int m = Integer.parseInt(s1[j]); edges[Integer.parseInt(s1[0]) - 1].add(vertices[m - 1]); } }

Answer1:

<strong>Method1</strong> is not going to end ever as below readLine() will never returns null, it will always return empty String while reading from console input.

while ((line = bufferedReader.readLine()) != null) { para = para + line; }

Remember on pressing enter java considers it as newLine() command, which surely not null, so your program goes on with empty string and never terminates from the while loop.

I would suggest to have some meaning full check to terminate while loop. You can try Scanner instead of BufferedReader as it provides lot of utility methods.

In Scanner you have methods like hasNext(), hasNextLine() etc which tells whether you have further input or not. Similarly methods like nextLine(), nextInt(), nextDouble() are there to get specific converted values from the command line or any other stream.

<strong>Method2</strong> is doing lot of operations which were not part of the Method1, can you please share full runnable code for the same. I doubt on the logic again for terminating the loop.

So I would suggest to use more specific check to terminate your loop while iterating through some data.

Answer2:

When you press the entre key the bufferedReader object receive an empty string so it's not null.

To end the input should look for a key such as this :

private static String para = null; public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(System.in)); String line; while (!"end".equals((line = bufferedReader.readLine()))) { para = para + line; } bufferedReader.close(); System.out.println(para); }

here I choose 'end' as a ending sequence but you can choose anything such as empty string wich is the result for pressing enter on an empty line.

Answer3:

no output shows up why??

because you are not printing any thing inside loop

while (!(line = bufferedReader.readLine()).equals("")) // read until enter without inputting anything is pressed.

private static String para = ""; // initialize your para with empty string(i.e "") not with null otherwise null will be added to your para string. public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(System.in)); String line=bufferedReader.readLine(); do{ System.out.println(line); // print what you input para = para + line; } while (!(line = bufferedReader.readLine()).equals("")); bufferedReader.close(); System.out.println(para); }

Recommend

  • I want to write a generic function where for these valid date format MM/dd/yyyy, M/dd/yyyy or M/d/yy
  • How to read data from socket connection - android
  • C# where to add a method
  • Zend Framework 2, Module Redirect
  • Why does PHP appear to evaluate this condition incorrectly?
  • How to fail Phing without triggering backtrace
  • vi mode to emacs mode while on R
  • Contact form problem - I do receive messages, but no contents (blank page)
  • Linq Full Outer Join on Two Objects
  • How to get the index of element in the List in c#
  • If statement skipping right to else after being called once?
  • Reading a file into a multidimensional array
  • App restarts from wrong activity
  • jQuery ready not fired after rails link_to is clicked
  • How to use RequestBodyAdvice
  • Time complexity of a program which involves multiple variables
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Android full screen on only one activity?
  • Yii2: Config params vs. const/define
  • $wpdb not working in file of WordPress plugin
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • Jetty Server not starting: Unable to establish loopback connection
  • how to adjust image in a panel in Java swing?
  • Why value captured by reference in lambda is broken? [duplicate]
  • Array.prototype.includes - not transformed with babel
  • Optimizing database types to compact database (SQLite)
  • TFS: Get latest causes slow project reloading
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • Running a C# exe file
  • sending/ receiving email in Java
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • How does Linux kernel interrupt the application?
  • unknown Exception android
  • Checking variable from a different class in C#
  • Reading document lines to the user (python)
  • To Get the radio button value in ruby on rails