• Status: Solved
• Priority: Medium
• Security: Public
• Views: 215

# Numeric value of a string

Hello,

I want to compare two strings for instance the first string holds the value "ABC" and the second string holds "ABD", But i want to find the numeric value of the string and then compare the two. I have used value of but it has not worked.

Cheers
0
Cyart
• 6
• 4
• 2
• +3
2 Solutions

Software engineerCommented:
String s1 = "ABC";
String s2 = "ABD";
Integer i1 =  Integer.valueOf(s1, 16);
Integer i2 =  Integer.valueOf(s2, 16);
0

Author Commented:
What does the 16 represent?
0

Software engineerCommented:
the radix to be used in interpreting the string
0

Software engineerCommented:
I assumed that "ABC" stands for the hexadecimal value x0abc, right?
0

Software engineerCommented:
>>What does the 16 represent?
>>the radix to be used in interpreting the string
See http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html
0

Author Commented:
No I want the deciamal value based on ascii i.e A = 65 so that ABC would give me 198.

Cheers
0

Commented:
int i=0;

for(int k=0;k<str.length();k++)
{
i=i+str.chatAt(k);
}

int j=0;
for(int k1=0;k1<str1.length();k1++)
{
j=j+str1.chatAt(k1);
}
//now u can compare j and i here
0

Commented:
where str="ABC";

where str1=="ABD";
0

Commented:
String.compareTo(String otherString);  would serve you better then .....
;JOOP!
0

Commented:
String str="ABC";
String str1="ABD";

int i=0;

for(int k=0;k<str.length();k++)
{
i=i+str.chatAt(k);
}

int j=0;
for(int k1=0;k1<str1.length();k1++)
{
j=j+str1.chatAt(k1);
}

System.err.println(i+" " +j);
0

Commented:
sciuriware

I just want to ask one question.  What is meaning of this ;JOOP!
0

Commented:
compareTo() in String might be all that you need to use.  It compares two strings lexicographically.  It will return 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.  It doesn't just return -1, 0, and 1 though; the actual number that is returned is relational to the distance lexicographically that the two Strings are from each other.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String)
0

Commented:
Oh oh, here we go again:

Joop (dutch) == Johan (german) == John (british) == Johannes (Roman) == The one who was promised to us.

You're better off as the one who they wanted in the first place  ....................

;JOOP!
0

Commented:
>>
for(int k=0;k<str.length();k++)
{
i=i+str.chatAt(k);
}

>>

should be

for(int k=0;k<str.length();k++)
{
i=i+str.charAt(k);
}

Maybe you were feeling talkative at the time sudhakar? ;-)

0

Commented:
:-))
0

Commented:
I am poor in using keyboard

as r and t are adjacent keys, I get type-o error

when keys are adjacent, I always face such  problems ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.