wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to combine the results after using if statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to combine the results after using if statement" Watch "how to combine the results after using if statement" New topic
Author

how to combine the results after using if statement

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
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

Joined: Oct 13, 2005
Posts: 38437
    
  23
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

Joined: May 30, 2012
Posts: 679
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

Joined: Jan 23, 2012
Posts: 199
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

Joined: Mar 17, 2011
Posts: 7677
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38437
    
  23
And why have you changed your code to use a tokenizer? You ought not to use string tolenizers any more.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38437
    
  23
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.
 
 
subject: how to combine the results after using if statement