I am confused about StringBUilder.append(). Netbeans say that void type not allowed.
Here is the the my Java class named Levencthein.java
public void printDistance(String s1, String s2) {
System.out.println( computeEditDistance(s1, s2) + " ("+similarity(s1, s2) * 100 +")");
}
now, in the other class, assumpted in b.java
StringBuilder s = new StringBuilder();
for (final JCCDFile file : files) {
LevenshteinDistance lv = new LevenshteinDistance();
if (idLexerSelection != getIDLexer()) {
System.out.println(temp.getName() + " ==> " + file.getName());
s.append(lv.printDistance(name1, name2)); This is the error
sb.append(file.getName());
}
}
any solution ?
You are attempting to append the return value of printDistance
, but there is no return value because it's void
. In printDistance
, instead of printing the value, return it.
return computeEditDistance(s1, s2) + " ("+similarity(s1, s2) * 100 +")";
You cannot append the void return of printDistance
to a StringBuilder
(as the compiler is telling you). If I understand you, then this
public void printDistance(String s1, String s2) {
System.out.println( computeEditDistance(s1, s2)
+ " ("+similarity(s1, s2) * 100 +")");
}
Should return String
and I'd return String.format
like
public void printDistance(String s1, String s2) {
return String.format("%s (%s)", computeEditDistance(s1, s2),
similarity(s1, s2) * 100);
}
Assuming that computeEditDistance()
and similarity()
return String
. If they return int
replace the %s
in format with %d
(or %f
for floating point).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.