Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String compare: Odd value out

 
Prabhudatta Choudhury
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Need a very small help. I am stuck with some small and fundamental issue in program which I'm not able to figure it out. Please help me here.

I have two string values as below.

String acctNo = "12345A";
String comparedTo = "0123456789"

I have to compare both and get the odd char out (e.g in this case 'A' is the char which stands out).

Note: That char value "A" can be anywhere in that String: means, it can a first char, or in middle or in last. There may be repetition on the same like "1234AD5"

Basically I want my String to contain only int value not any alphabet.

I tried to iterate the String by making to charAt(i) but failed to get the expected result.

Please help.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhudatta Choudhury wrote:
I tried to iterate the String by making to charAt(i) but failed to get the expected result.


If you want help with that, you'll need to provide 3 things:

1) Exactly what you tried.

2) Exactly what result you expected.

3) Exactly what result you observed.

In other words, please TellTheDetails(⇐click).
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhudatta Choudhury wrote:
I have to compare both and get the odd char out (e.g in this case 'A' is the char which stands out).


Try like this:

[Moderator action: Full code solution removed. surlac surlacovich, please remember, this site is NotACodeMill(←click). Thanks!]
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
We do not permit full answers, at least until the OP has tried very hard themselves.

I presume in the two Strings mentioned, the 1 is found as the first character differing.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to find the first letter in a String, that is quite easy. So is it to find the first character where the two Strings differ. But your original post seems to confuse the two.
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't post a code - that's crazy! It's programming forum, what if it's much easier to explain with code than with the whole poem of words-words-words.

@OP: you can even iterate through every character of the string while your index is less that length of that string and take every character by the index to do whatever you want (put to another string to build new one, replace, etc.).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
surlac surlacovich wrote:I can't post a code - that's crazy! It's programming forum, what if it's much easier to explain with code than with the whole poem of words-words-words.


Posting an example that demonstrates a concept the OP is trying to understand is acceptable, if it's different enough from his problem.

Doing the OP's work for him is not acceptable. This site is NotACodeMill(⇐click).

Telling him exactly what to do in prose is not generally helpful either.

People learn better when you nudge them in the right direction, giving them enough information to set them on the path to solving their own problems, but still letting them work out most of it for themselves.
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prabhudatta Choudhury,

you are asking that how to know the different characters in two strings. if am right in understanding your question then,

use charAt() method on two strings and get the character from two strings and compare them if they are are not equal then print it.


Regards,
James
 
Winston Gutkowski
Bartender
Pie
Posts: 10236
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
surlac surlacovich wrote:I can't post a code - that's crazy! It's programming forum, what if it's much easier to explain with code than with the whole poem of words-words-words...

1. Them's the rules - for all the reasons Jeff explained.
2. Who cares if it's easier? That's not the reason this site exists. It's here to help people; and you don't do that by spoon-feeding them answers.

Winston
 
Winston Gutkowski
Bartender
Pie
Posts: 10236
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhudatta Choudhury wrote:Basically I want my String to contain only int value not any alphabet.

No, that's NOT what you want, except in the specific example you gave.

My advice: StopCoding (←click) and think about the problem.

What is it exactly that makes 'A' the "odd man out" in your example?

What if there's more than 1 "odd man out"? What do you want to do then?

What if there are duplicate characters in your Strings? What's an "odd man out" then?

You must be clear about what you want to do before you write a line of Java code.

Winston
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
2. Who cares if it's easier? That's not the reason this site exists. It's here to help people; and you don't do that by spoon-feeding them answers.

That makes sence. I like the idea of this site. I'll try to get used to it.

@OP: you can even use replaceAll() method of java.lang.String to filter out chars by regex. But you need to know regular expressions, which is kind of cryptic for a novice - personally I use reference book while building one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic