*
The moose likes Beginning Java and the fly likes String compare: Odd value out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String compare: Odd value out " Watch "String compare: Odd value out " New topic
Author

String compare: Odd value out

Prabhudatta Choudhury
Greenhorn

Joined: Jul 03, 2009
Posts: 20

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.


Thanks
Prabhudatta
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 12, 2013
Posts: 296

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

Joined: Oct 13, 2005
Posts: 37902
    
  22
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

Joined: Oct 13, 2005
Posts: 37902
    
  22
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

Joined: Mar 12, 2013
Posts: 296

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 26, 2013
Posts: 158
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

Joined: Mar 17, 2011
Posts: 7492
    
  18

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

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

Joined: Mar 17, 2011
Posts: 7492
    
  18

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

Joined: Mar 12, 2013
Posts: 296

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String compare: Odd value out
 
Similar Threads
Converting a string to a double
Regarding Login
Methods
Hangman BufferReader ??
efficient algorithm?