• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing two strings  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I want to compare two strings. They look like this:

String str1= "RXN0174 TCPD0791 RXN0175 TCPD0793 RXN0176 TCPD0795 RXN0177 TCPD0797 RXN0178 TCPD0789" ;

String str2= "CPD1045 TCPD0789 TCPD1201 CPD0169 TCPD1108 TCPD0793 CPD0135 TCPD1110 TCPD1109 CPD1033 TCPD1797 CPD1495 TCPD1497 TCPD1496 CPD1271 TCPD0789";

I want to do the following:

1. Read through str1.

2. Find instances of TCPDXXXX.

3. If a given TCPDXXXX is in str2, then write the RXNXXXX before TCPDXXXX in str1, along with the CPDXXXX before TCPXXXX in str2. Then skip line.

4. If a given TCPXXXX is not in str2, then just write RXNXXXX TCPDXXXX as in str1. Then skip line.

So the output should like this:

RXN0174 TCPD0791
RXN0175 CPD0169
RXN0176 TCPD0795
RXN0177 TCPD0797
RXN0178 CPD1271


Does that make sense?

Any ideas on how to accomplish my task?

Thanks,

kmath
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Merfeld wrote:Does that make sense?

Yes, but only because you supplied your expected output (which is good ).

Any ideas on how to accomplish my task?

Yes again.

Go back to your description of the problem and work out what's missing.

A good exercise for that is to imagine you're trying to explain it to a 10-year old, who has no knowledge of Java.

THEN sit down with a pencil and paper and work out how you're going to do it. And don't write a line of Java code until you do.

Programs don't "materialise" by coding. You need to understand the problem.

Winston
 
Kevin Merfeld
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:

Go back to your description of the problem and work out what's missing.

A good exercise for that is to imagine you're trying to explain it to a 10-year old, who has no knowledge of Java.

THEN sit down with a pencil and paper and work out how you're going to do it.


Winston



Let's try this then:

1. Read through str1.

2. Find instances of TCPDXXXX.

3. Make a list of all the TCPDXXXXs in str1.

4. Now read str2 and look for the first TCPDXXXX in your list.

5. If the TCPDXXXX you are looking for is in str2, then write the RXNXXXX before TCPDXXXX in str1, along with the CPDXXXX before TCPXXXX in str2. Then skip line.

6. If the TCPDXXXX you are looking for is not in str2, then just write RXNXXXX TCPDXXXX as in str1. Then skip line.

7. Now read str2 again and look for the next TCPDXXXX in your list.

8. Repeat steps 5 and 6.

9. Repeat steps 7 and 8 until you have read all the TCPDXXXXs is your list.



Better?



 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Merfeld wrote:
Better?



How about this? This is step one.

Kevin Merfeld wrote:
1. Read through str1.


Do you understand this?

Remember, that you will need to write code for this later. You can choose to get as detailed as possible, to make your coding tasks later easier, or you can choose to make your task easier now, in order to make your coding task later harder.... so, is it better? That is for you to decide?

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Merfeld wrote:Better?

Much.

Now follow Henry's advice and make sure you understand what "reading through" is all about. You might find the String.split() method useful for a problem like this.

Winston
 
Kevin Merfeld
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:

Now follow Henry's advice and make sure you understand what "reading through" is all about. You might find the String.split() method useful for a problem like this.

Winston


I think I actually figured that part out. This should cover steps 1-3 (from above) :



It's step 4 that's the issue. That is, how do I look through str2 for instances of TCPDXXXX in the list store1? Does that make sense?

KM
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Merfeld wrote:
It's step 4 that's the issue. That is, how do I look through str2 for instances of TCPDXXXX in the list store1? Does that make sense?


That's the whole point of the exercise. If you hit a step that you can't figure out, then obviously, a 10-year old won't understand it either.

You need to go back to re-elaborate that step -- because if you can't do it on paper, how are you expected to write code to do it?

Henry
 
Kevin Merfeld
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

That's the whole point of the exercise. If you hit a step that you can't figure out, then obviously, a 10-year old won't understand it either.

You need to go back to re-elaborate that step -- because if you can't do it on paper, how are you expected to write code to do it?

Henry


I disagree. I can do it on paper, very easily. And I think a 10 year-old could too, given the directions I gave. The difficult part is telling Java to do it.
 
Kevin Merfeld
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another step I've done:

 
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's step 4 that's the issue. That is, how do I look through str2 for instances of TCPDXXXX in the list store1? Does that make sense?

You could look at the API docs for the String class and see if there are any methods that might hep you.

Here's another step I've done:

Are you sure you want to extract the instances of TCPDXXXX from the string. It makes it hard to get the preceding data block especially if your string has more than one TCPD data block with the same number.
Earlier Winston suggested using String.split() which would certainly make it easier to get the preceding data block.
 
Kevin Merfeld
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Are you sure you want to extract the instances of TCPDXXXX from the string. It makes it hard to get the preceding data block especially if your string has more than one TCPD data block with the same number.
Earlier Winston suggested using String.split() which would certainly make it easier to get the preceding data block.


I tried another intermediate step using your suggestion. I think I'm on the right track now:

 
Kevin Merfeld
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally cracked it. Thanks a lot for your help everyone!
 
Kevin Merfeld
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any way I can give a couple of you some points up?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Merfeld wrote:Finally cracked it. Thanks a lot for your help everyone!

Glad to hear you have sorted it and thanks for the thanks and thumbs up.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW A useful exercise would be to look back at your list of steps to solve the problem and see how you can improve them to better describe a solution in simple steps.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Merfeld wrote:Any way I can give a couple of you some points up?

Yup.

<mercenary>
Just click on the +1 button if you think the post helped.
</mercenary>


Winston
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satheesh chandragupthan,
Your post was moved to a new topic.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!