string-question

P

pet0etie

hello,

is there some kind of function implemented in java to compare 2 strings,
which returns me a double with a value that measures the equality of 2
strings

let's say ...
<code>
String str1;
String str2;
double a;

str1 = "abcdef";
str2 = "abcdef";
a = str1.equality(str2);
// a = 1.000;

str1 = "abcdef";
str2 = "abccdef";
a = str1.equality(str2);
// a = 0.800;

str1 = "abcdef";
str2 = "abcccdef";
a = str1.equality(str2);
// a = 0.750;

str1 = "abcdef";
str2 = "abccef";
a = str1.equality(str2);
// a = 0.850;
</code>

or do i have to implement such a function on my own ?
i know ... maybe something that will be rarely used ... but i need something
like this

anyone who can help me or point to some functions i might need ?

thank u in advance,
pet0etie
 
V

VisionSet

is there some kind of function implemented in java to compare 2 strings,
which returns me a double with a value that measures the equality of 2
strings

String str1;
String str2;
double a;

str1 = "abcdef";
str2 = "abcdef";
a = str1.equality(str2);
// a = 1.000;

str1 = "abcdef";
str2 = "abccdef";
a = str1.equality(str2);
// a = 0.800; ....

or do i have to implement such a function on my own ?

You will have to implement it yourself.
Only you can determine the significance of e.g. 0.8 for 'partial' equality.
 
D

Daniel Chirillo

You can compare strings using compareTo(), which returns an int. A return
value of 0 indicates equality.
 
A

Ann

Daniel Chirillo said:
You can compare strings using compareTo(), which returns an int. A return
value of 0 indicates equality.

I was thinking about compareTo() also, but since the OP wants a double
s/he has to create her own compareTo() that returns a double. Now my
question is "how will the sort methods know to call the double version
instead of the int version?" Or, maybe I am a bit mixed up on this.
 
V

VisionSet

Ann said:
I was thinking about compareTo() also, but since the OP wants a double
s/he has to create her own compareTo() that returns a double. Now my
question is "how will the sort methods know to call the double version
instead of the int version?" Or, maybe I am a bit mixed up on this.

You cannot override a method with the same signature and a different return
type.

ie you cannot do this:

int myMethod();
double myMethod();
 
V

VisionSet

Tony Morris said:
Yes you can.
See JSR-14 and/or J2SE 5.0 "co-variant return types".

Okay, that is excellent - another reason to get my arse in gear and update
to 5.0
 
P

Peter Kirk

VisionSet said:
Okay, that is excellent - another reason to get my arse in gear and update
to 5.0

But remember that in this particular case you can't "override" the compareTo
method, because String is final.
 
V

VisionSet

Peter Kirk said:
But remember that in this particular case you can't "override" the compareTo
method, because String is final.

Of course, but you can implement a Comparator and use this for sorting.
However overloading the compareTo method will not make it executable at
runtime by Collections sorting mechanism.
 
P

pet0etie

thanks for all the reactions and good thinking ...
i examined all of the suggestions and was really pleased with the
"Levenshtein distance"-theory
i was thinking in a completely other direction but i think the offered one
looks better

again : thank u all
pet0etie-back-to-programming
 
P

pet0etie

"Levenshtein distance"-theory

as i promised earlier today i was intended to use the "levenshtein
distance"-theory to determine the similarity of 2 strings ... i just
finished the work and even with not a single error i got a perfect working
result

thanks to everybody who helped me to achieve this

greetz,
pet0etie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,771
Messages
2,569,587
Members
45,097
Latest member
RayE496148
Top