我如何修改此客户端 class 以从字符串数组(而不是标准输入)读取输入?
How do I modify this Client class to read input from an array of Strings (rather than from standard input)?
我在 Java 客户端 class 中有以下代码(就是这个 KnockKnock client/server pair ):
try {
kkSocket = new Socket("localhost", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = KKJokes[0]; //new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
// in2 = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
}
我想让它自动从字符串数组(包含所有那些预定义的笑话)中读取,像这样:
String KKJokes[] = {"Who's there?", "Turnip who?",
"y", "Who's there?", "Who's there?", "blah" };
/* more cod */
try {
kkSocket = new Socket("localhost", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
for (int i = 0; i< KKJokes.length; i++) {
in = new BufferedReader( new InputStreamReader( KKJokes[i] )) ;
}
}
但这不起作用,它给出了:
KnockKnockClientRedux.java:33: non-static variable KKJokes cannot be referenced from a static context
for (int i = 0; i< KKJokes.length; i++) {
^ KnockKnockClientRedux.java:34: non-static variable KKJokes cannot be referenced from a static context
in = new BufferedReader( new InputStreamReader( KKJokes[i] )) ; //new Buf feredReader(new InputStreamReader(kkSocket.getInputStream()));
^ KnockKnockClientRedux.java:34: cannot find symbol symbol : constructor InputStreamReader(java.lang.String) location: class java.io.InputStreamReader
in = new BufferedReader( new InputStreamReader( KKJokes[i] )) ; //new Buf feredReader(new InputStreamReader(kkSocket.getInputStream()));
编辑:从文本文件中读取不是更容易吗?也许我会在文本文件中垂直列出所有 KnockNock 笑话台词。这样,似乎更容易将 FileReader
换成 kkSocket.getInputStream()
代码
编译器消息说明了一切。试试
static String KKJokes[] = {"Who's there?", "Turnip who?",
"y", "Who's there?", "Who's there?", "blah" };
第二个问题可以这样解决:
new InputStreamReader( new ByteArrayInputStream(KKJokes[i].getBytes() )
我在 Java 客户端 class 中有以下代码(就是这个 KnockKnock client/server pair ):
try {
kkSocket = new Socket("localhost", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = KKJokes[0]; //new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
// in2 = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
}
我想让它自动从字符串数组(包含所有那些预定义的笑话)中读取,像这样:
String KKJokes[] = {"Who's there?", "Turnip who?",
"y", "Who's there?", "Who's there?", "blah" };
/* more cod */
try {
kkSocket = new Socket("localhost", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
for (int i = 0; i< KKJokes.length; i++) {
in = new BufferedReader( new InputStreamReader( KKJokes[i] )) ;
}
}
但这不起作用,它给出了:
KnockKnockClientRedux.java:33: non-static variable KKJokes cannot be referenced from a static context
for (int i = 0; i< KKJokes.length; i++) {
^ KnockKnockClientRedux.java:34: non-static variable KKJokes cannot be referenced from a static context
in = new BufferedReader( new InputStreamReader( KKJokes[i] )) ; //new Buf feredReader(new InputStreamReader(kkSocket.getInputStream()));
^ KnockKnockClientRedux.java:34: cannot find symbol symbol : constructor InputStreamReader(java.lang.String) location: class java.io.InputStreamReader
in = new BufferedReader( new InputStreamReader( KKJokes[i] )) ; //new Buf feredReader(new InputStreamReader(kkSocket.getInputStream()));
编辑:从文本文件中读取不是更容易吗?也许我会在文本文件中垂直列出所有 KnockNock 笑话台词。这样,似乎更容易将 FileReader
换成 kkSocket.getInputStream()
代码
编译器消息说明了一切。试试
static String KKJokes[] = {"Who's there?", "Turnip who?",
"y", "Who's there?", "Who's there?", "blah" };
第二个问题可以这样解决:
new InputStreamReader( new ByteArrayInputStream(KKJokes[i].getBytes() )