Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to combine the results after using if statement

 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Suppose i have an input like this

R1 : (bus,train)
R2 : (bicycle,bike) R1
R3 : (lorry,tractor) R1

I have replaced the words R1 with (bus,train) and got the result.

Now i would like to have the combined output all into one after using the if else i need the combined output of both . How to make it achievable ?



This is the code for replacement. How can i combine the if and else output into one and get the result? Suggest me
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find it difficult to understand your question. You would appear to want to run the if and the else, combining their “output”. You would do well to play with replaceAll, and see what happens when you do and do not have R1 inside the String. I suspect the if-else is unnecessary.
You might do well to investigate Maps; R1 = bus/train looks like something a Map is very good at implementing.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But to give a general answer to your question - if you are building up a string section by section depending on a number of conditions, the usual approach would be to append the values to a StringBuilder instance as you go along.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I find it difficult to understand your question. You would appear to want to run the if and the else, combining their “output”. You would do well to play with replaceAll, and see what happens when you do and do not have R1 inside the String. I suspect the if-else is unnecessary.
You might do well to investigate Maps; R1 = bus/train looks like something a Map is very good at implementing.


ya your correct in that case it is not necessary . If the input is given from a string instead to direct place. I think it is necessary



in this case the values are printed multiple times. So i would like to use if.contains(res) and else part separate the output. So i am looking to get the combined result of both. I hope you will understand why i am using if else in above.
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote:I hope you will understand why i am using if else in above.

Not really because, as usual, you haven't provided us with any context.

Your questions always seem to be of the form:
"I've written this code ... is it OK?"
And the answer (as it almost always will be in such a case) is:
It depends on what you're trying to do.

For the third time, I'm going to give you a few links on how to ask questions properly:
1. HowToAskQuestionsOnJavaRanch.
2. How to ask questions the smart way.
3. How to provide an SSCCE.
and I strongly suggest that you read them; because, until you do, programming is likely to be a very frustrating experience for you - and us.

Winston
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why have you changed your code to use a tokenizer? You ought not to use string tolenizers any more.
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote: . . . I hope you will understand why i am using if else in above.
Not at all. that makes the question even more confusing, as Winston has already said.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic