Java基本控制台编程 - 可以使用hasNextLine从控制台读取输入? [英] Java basic console programming - can use hasNextLine to read inputs from console?
问题描述
我已经看到这个代码对我看起来不自然。我通常使用 hasNextLine()
,而不是一个布尔变量 done
,如 while
loop,但现在我很困惑。我的问题是,我可以替换的逻辑,其中变量完成
显示与 hasNextLine()
控制台,或者当输入来自文件时,我可以只使用 hasNextLine()
吗?这是一个更好的实践方法,通过使用 done
变量或 hasNextLine()
?感谢。
// TCPClient.java
import java.net。
import java.io. *;
import java.lang。*;
public class TCPClient {
public static void main(String args []){
Socket clientSock = null;
try {
int port_num = Integer.valueOf(args [1])。intValue(); //获取服务器的端口号。
clientSock = new Socket(args [0],(int)port_num); // args [0]是服务器主机名
/ * String sock = clientSock.toString();
System.out.println(sock); * /
PrintWriter oStream =
new PrintWriter(clientSock.getOutputStream(),true);
BufferedReader iStream =
new BufferedReader(new InputStreamReader
(clientSock.getInputStream()));
BufferedReader keyInput =
new BufferedReader(new InputStreamReader(System.in));
boolean done = false;
String answer = iStream.readLine();
if(answer!= null)
System.out.println(answer);
while(!done){
String line = keyInput.readLine();
if(line.trim()。equals(BYE))
done = true;
oStream.println(line);
answer = iStream.readLine();
if(answer!= null)
System.out.println(answer);
}
clientSock.close();
} catch(Exception e){
System.out.println(e);
}
}
}
@ Voo的解决方案中有一个错误,因为 nextLine()
在 keyInput
c $ c> null 。这是一个更正的版本:
字符串行;
while((line = keyInput.readLine())!= null&
!line.trim()。equals(BYE)){
oStream.println );
answer = iStream.readLine();
if(answer!= null){
System.out.println(answer);
}
}
当我们希望从控制台输入时,我可以用
hasNextLine()
来显示变量done的逻辑,或者我只能使用hasNextLine()
当输入来自一个文件?
您可以封装任何 InputStream
或任何
(其中 Scanner
中的 Reader
是子类型),允许您使用 hasNextLine()
。唯一的警告是,如果底层流来自控制台,管道,套接字等, hasNextLine()
可能会阻止无限等待输入。
这是一个更好的实践方法来实现这个代码,其中输入来自控制台,通过使用
done
变量或
hasNextLine()
?
,如上所述的第三选项。这真的是一个味道的问题...和你认为看起来最简单。 (个人而言,我不会使用 Scanner
,以便我可以调用 hasNextLine()
$ c> BufferedReader 是 Scanner
隐藏任何可能在呼叫中出现的 IOException
到 hasNext ...()
并返回 false
。这对于典型的使用情况来说是一件好事 - Scanner
作为一个轻量级的用户输入解析器(因为你在 keyInput
),但可能不在其他用例中。
I have been presented with this code which looks unnatural to me. I would normally use hasNextLine()
rather than a Boolean variable done
as shown in this code in the while
loop, but now I'm confused. My question is, can I replace the logic where the variable done
is shown with hasNextLine()
when input is expected from the console, or can I only use hasNextLine()
when input comes from a file? Which is a better practice way of implementing this code where input comes from console, by using the done
variable or hasNextLine()
? Thanks.
// TCPClient.java
import java.net.*;
import java.io.*;
import java.lang.*;
public class TCPClient{
public static void main(String args[]){
Socket clientSock=null;
try{
int port_num = Integer.valueOf(args[1]).intValue(); //get server's port no.
clientSock = new Socket(args[0],(int)port_num); // args[0] is the server host name
/* String sock=clientSock.toString();
System.out.println(sock); */
PrintWriter oStream =
new PrintWriter(clientSock.getOutputStream(),true);
BufferedReader iStream =
new BufferedReader(new InputStreamReader
(clientSock.getInputStream()));
BufferedReader keyInput =
new BufferedReader(new InputStreamReader(System.in));
boolean done = false;
String answer = iStream.readLine();
if(answer != null)
System.out.println(answer);
while(!done){
String line = keyInput.readLine();
if(line.trim().equals("BYE"))
done = true;
oStream.println(line);
answer = iStream.readLine();
if(answer != null)
System.out.println(answer);
}
clientSock.close();
}catch(Exception e){
System.out.println(e);
}
}
}
There is a bug in @Voo's solution because the nextLine()
on keyInput
could also return null
. Here's a corrected version:
String line;
while ((line = keyInput.readLine()) != null &&
!line.trim().equals("BYE")) {
oStream.println(line);
answer = iStream.readLine();
if (answer != null) {
System.out.println(answer);
}
}
Can I replace the logic where the variable done is shown with
hasNextLine()
when input is expected from the console, or can I only usehasNextLine()
when input comes from a file?
You can wrap any InputStream
or any Readable
(of which Reader
is a subtype) in a Scanner
, allowing you to use hasNextLine()
on all of them. The only caveat is that hasNextLine()
can potentially block indefinitely waiting for input if the underlying stream comes from a console, pipe, socket or similar.
Which is a better practice way of implementing this code where input comes from console, by using the
done
variable orhasNextLine()
?
Either will do, as will the third option as illustrated above. It is really a matter of taste ... and what you think looks simplest. (Personally, I'd not use Scanner
just so that I can call hasNextLine()
... but that's just my opinion.)
The other significant difference between using Scanner
and BufferedReader
is that Scanner
hides any IOException
s that might occur in a call to hasNext...()
and returns simply false
. This is a good thing for typical use-cases of Scanner
as a light-weight user input parser (as you are using it on keyInput
), but maybe not in other use-cases.
这篇关于Java基本控制台编程 - 可以使用hasNextLine从控制台读取输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!