Solved

Numeric value of a string

Posted on 2004-10-12
16
207 Views
Last Modified: 2008-03-10
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
Comment
Question by:Cyart
  • 6
  • 4
  • 2
  • +3
16 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12288082
String s1 = "ABC";
String s2 = "ABD";
Integer i1 =  Integer.valueOf(s1, 16);
Integer i2 =  Integer.valueOf(s2, 16);
0
 

Author Comment

by:Cyart
ID: 12288119
What does the 16 represent?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12288175
the radix to be used in interpreting the string
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 37

Expert Comment

by:zzynx
ID: 12288188
I assumed that "ABC" stands for the hexadecimal value x0abc, right?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12288203
>>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 Comment

by:Cyart
ID: 12288257
No I want the deciamal value based on ascii i.e A = 65 so that ABC would give me 198.

Cheers
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12288314
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12288317
where str="ABC";

where str1=="ABD";
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 63 total points
ID: 12288323
String.compareTo(String otherString);  would serve you better then .....
;JOOP!
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12288335
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12288351
sciuriware

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

Accepted Solution

by:
Breadstick earned 62 total points
ID: 12289319
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.

This may be helpful:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12289410
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12289811
>>
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12289849
:-))
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12289883
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
Spring Framework HTTPSession management 1 33
servlet example 11 40
eclipse apache tomcat admin console 52 95
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question