1

I want to append a string at runtime but this 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.

2
  • There is no such object as __Customer? Also very strange naming scheme of instance variables. Capital letters? Commented Nov 19, 2010 at 8:07
  • Why do you use getCustomerQuestion on an object named _ITChatParticipant when you use _Customer everywhere else in your example? Is it a typo? Commented Nov 19, 2010 at 8:09

2 Answers 2

2

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

@singh: It's hard to help, given that it's clearly a problem in Request.getInput() and you've given us no information at all about what that is.
0

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.

3 Comments

It's not at all clear whether that's appropriate, and it certainly doesn't explain the behaviour the OP is seeing.
Its better to delete this post, before somebody come and suggest StringBuilder. ;)
String concatenation being a "bottleneck" in this application is certainly going to be the least of the OPs problems

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.