Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

how to combine the results after using if statement

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic