English 中文(简体)
Removing pause after reading using readLine in java
原标题:

I am reading several lines from the console in a java program using the readLine command from the BufferedReader class. However, the program pauses at the end of the last line and does not proceed until i press return. How do I eliminate this pause?

最佳回答

What exactly do you do and what do you expect? I have two guesses:

  1. You re having a superfluous readLine() at the end of the program. Remove it or change loop conditions appropriately to solve this.
  2. You expect the program to start work on a line that is being entered (without hitting Return at the end). That s where your expectations are dead-wrong. readLine() returns a full line and blocks while it is being entered. So the call just waits until you hit Return and then returns the line you just entered. To get input while the user is typing neither BufferedReader nor readLine() are very suitable.
问题回答

It s a catch-22; you cannot know it s the last line, until it s been fully entered (including the carriage return at the end) and you can parse it. So you only know that you could have skipped waiting once you ve already missed your chance.

That assumes, of course, that you detect the last line based on its content (e.g. empty string, specific "42" string, etc.). If you have some other means of detecting what the last line is, then you can use this as your while loop condition - hence you don t need to wait to read the "sentinel" line that simply tells you to exit.

But in the code example you posted, you only get access to the "42" line after it has been completely entered - including some line terminator (carriage return, EOF, etc.). Thus until you hit return, that line doesn t exist from the reader s perspective.

A line is a CR or CRLF terminiated sequence of characters. And readline() just does what it s name says: it reads a line.

If you have any other clue to determine end-of-input , you can read the input character by character until the end-of-input condition is true. This may be another character or a special sequence of characters like a closing tag if you look at an xml input.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签