C
Curts
Can anyone take a look at the code below? I have this method that is
supposed to return a string with only the first character of a word
uppercase. This is working fine except for a string with parens in
it.
public class paren {
public static String s = "STRING THAT (HAS PARENS) INSIDE OF IT.";
public paren() {
System.out.println("Before UCW: " + s);
System.out.println(" After UCW: " + this.ucwords(s));
}
public String ucwords(String str) {
char[] broken = str.toLowerCase().toCharArray();
StringBuffer buf = new StringBuffer();
boolean doUpperCase = true;
for (int i = 0; i < broken.length; i++) {
if (doUpperCase) {
buf.append(String.valueOf(broken).toUpperCase());
doUpperCase = false;
} else {
//if (broken == '(') <-- this doesn't work
if (String.valueOf(broken).equals("(")) // <--
doesn't work either
doUpperCase = true;
else if (broken == ' ')
doUpperCase = true;
buf.append(broken);
}
}
return buf.toString();
}
public static void main(String[] args) {
new paren();
}
}
Any ideas on what I might be doing wrong?
Cheers,
Curts
supposed to return a string with only the first character of a word
uppercase. This is working fine except for a string with parens in
it.
public class paren {
public static String s = "STRING THAT (HAS PARENS) INSIDE OF IT.";
public paren() {
System.out.println("Before UCW: " + s);
System.out.println(" After UCW: " + this.ucwords(s));
}
public String ucwords(String str) {
char[] broken = str.toLowerCase().toCharArray();
StringBuffer buf = new StringBuffer();
boolean doUpperCase = true;
for (int i = 0; i < broken.length; i++) {
if (doUpperCase) {
buf.append(String.valueOf(broken).toUpperCase());
doUpperCase = false;
} else {
//if (broken == '(') <-- this doesn't work
if (String.valueOf(broken).equals("(")) // <--
doesn't work either
doUpperCase = true;
else if (broken == ' ')
doUpperCase = true;
buf.append(broken);
}
}
return buf.toString();
}
public static void main(String[] args) {
new paren();
}
}
Any ideas on what I might be doing wrong?
Cheers,
Curts