Appending text in string
I want to append a string at runtime but t开发者_如何学Pythonhis code again add the previous word not to append new word.
Customer.java:
public String getCustomerQuestion() {
return CustomerQuestion;
}
public void setCustomerQuestion(String customerQuestion) {
CustomerQuestion = customerQuestion;
}
public void appendmessage(String msg){
CustomerQuestion = CustomerQuestion +" "+ msg;
}
Main.java:
Customer _Customer =new Customer();
Request_Message Request= new Request_Message;
_Customer.setCustomerQuestion(Request.getInput());
String _string=Request.getInput();
_Customer.appendmessage(_string);
String str=__Customer.getCustomerQuestion();
System.out.println("now new Question() is"+str);
When I write ram then press enter after again when I write singh it show result: ram ram on console. I want to show display ram singh as a string. `public class Request_Message { { private String _Input;
public void setInput (String line) { _Input = line; } public String getInput() { return _Input; }`
It takes the input from a chat window.
You haven't shown what Request.getInput()
does. I suspect that's the problem. What happens if you run:
System.out.println(Request.getInput());
System.out.println(Request.getInput());
and enter the two different strings?
If you could post a short but complete program, we could definitely work out what's going on.
(On a side note, if you follow normal Java naming conventions it's likely to be easier for others to follow your code.)
Don't use string concatenation. Use a proper StringBuffer
instead.
private StringBuffer customerQuestion = new StringBuffer();
public void appendmessage(String msg){
customerQuestion.append(msg);
}
public String getCustomerQuestion() {
return customerQuestion.toString();
}
Granted, this is probably not the cause of your specific issue (for that matter see Jon Skeet's answer), but this might be a bottleneck in the future. A StringBuffer
is the proper way of having a String
and concatenating text to it over time.
精彩评论