单个String可以包含多行吗? [英] Can single String contain multiple lines?
问题描述
例如,如果我将多行分配给字符串:
while((line = reader.readLine() )!= null)
{
输出+ =线+\ n;
}
我可以用行分隔符作为一个字符串返回输出吗? / p>
我正在编写一个具有客户端和服务器程序的Socket程序,其中客户端向服务器发送请求,服务器以String的形式将该请求返回给客户端,但是有些字符串是多行。
服务器程序代码(代码的一部分):
if(clinetChoice.equals(3))
{
String command =free;
流程进程= Runtime.getRuntime()。exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
System.out.println(你选择选项三);
字符串行;
while((line = reader.readLine())!= null)
{
output + = line;
System.out.println(line);
line = reader.readLine();
}
}
客户端程序代码:
while((fromServer = input.readLine())+\ n!= null)
{
System.out.println(Server:+ fromServer);
if(fromServer.equals(Bye))
break;
System.out.print(输入你的选择:);
fromClient = stdIn.readLine()。trim();
if(fromClient.equals(1))
{
System.out.println(Client:+ fromClient);
output.println(fromClient);
}
客户端程序中的fromServer从服务器程序输出。这适用于一行的输出,但如果它的多行,我无法弄清楚如何一次打印它。
所以如果输出例如等于:
一个
两个
三个
四个
它返回如下:
一美元b $ b输入您的选择:(它提示我输入新命令)
两个
输入您的选择:
三个
输入您的选择:
四个
所以它基本上打印一行,问我新的选择并且无关紧要我输入它打印第二行,然后是第三行,直到它到达最后一行,而不是像这样打印:
一个
两个
三个
四个
输入您的选择:
代码中还有另一个错误: while((fromServer = input.readLine())+\ n!= null)
。它将永远是真的。你应该只检查: while((fromServer = input.readLine())!= null)
。
如果我正确理解您的要求,您的代码应如下所示:
String fromServer =;
字符串行;
while((line = input.readLine())!= null){
fromServer + = line +\ n; //将多行字符串收集到fromServer
}
System.out.println(Server:+ fromServer);
if(fromServer.equals(Bye))
break;
System.out.print(输入你的选择:);
fromClient = stdIn.readLine()。trim();
if(fromClient.equals(1))
{
System.out.println(Client:+ fromClient);
output.println(fromClient);
}
For example if I assign multiple lines to a string as so:
while ((line = reader.readLine()) != null)
{
output += line + "\n";
}
Is it possible for me to return output with line separators as one String?
I'm writing a Socket program that has Client and Server program, where Client sends request to Server and server returns that request in form of a String back to Client, but some String are multiple lines.
Server Program code (part of code):
if (clinetChoice.equals("3"))
{
String command = "free";
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
System.out.println("You Chose Option Three");
String line;
while ((line = reader.readLine()) != null)
{
output += line;
System.out.println(line);
line = reader.readLine();
}
}
Client Program code:
while ((fromServer = input.readLine())+"\n" != null)
{
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye"))
break;
System.out.print("Enter your choice: ");
fromClient = stdIn.readLine().trim();
if(fromClient.equals("1"))
{
System.out.println("Client: " + fromClient);
output.println(fromClient);
}
fromServer in Client program is output from Server program. This works fine for output that's one line, but if its multiple lines I can't figure out how to print it all at once.
So if output for example equals:
One
Two
Three
Four
It returns as this:
One
Enter your choice: (It prompts me for new command)
Two
Enter your choice:
Three
Enter your choice:
Four
So it basically prints one line, ask me for new choice and doesn't matter what I enter it prints second line, then third line and so forth until it reaches last line, instead of printing like this:
One
Two
Three
Four
Enter your choice:
There is another mistake in code: while ((fromServer = input.readLine())+"\n" != null)
. It will be always true. You should only check: while ((fromServer = input.readLine()) != null)
.
Also if I understand your requirements correctly, your code should be something like below:
String fromServer = "";
String line;
while ((line = input.readLine()) != null) {
fromServer += line + "\n"; // collect multiline strings into fromServer
}
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye"))
break;
System.out.print("Enter your choice: ");
fromClient = stdIn.readLine().trim();
if(fromClient.equals("1"))
{
System.out.println("Client: " + fromClient);
output.println(fromClient);
}
这篇关于单个String可以包含多行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!