Must be a dumb mistake in char array loop

R

rcurts

What I'm trying to do:
Take any string and uppercase the first character of each word, leaving
all other characters lowercase
ex: "THIS IS A STRING" becomes "This Is A String"

Problem:
I can get the method I created to uppercase the first char of a word if it
is separated by a space, however when I try to do the same with a
paren in the string, the next character is not set to uppercase.

Code Example:
--------------------------------------------------------------------
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 (String.valueOf(broken).equals("("))
doUpperCase = true;
else if (broken == ' ')
doUpperCase = true;
buf.append(broken);
}
}

return buf.toString();
}

public static void main(String[] args) {
new paren();
}
}
--------------------------------------------------------------------

There must be a silly mistake here, but I can't find it.
Any ideas?

Cheers,
Curts
 
R

rzed

What I'm trying to do:
Take any string and uppercase the first character of each word,
leaving all other characters lowercase
ex: "THIS IS A STRING" becomes "This Is A String"

Problem:
I can get the method I created to uppercase the first char of a
word if it is separated by a space, however when I try to do the
same with a paren in the string, the next character is not set to
uppercase.

Code Example:
--------------------------------------------------------------------
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 (String.valueOf(broken).equals("("))
doUpperCase = true;
else if (broken == ' ')
doUpperCase = true;
buf.append(broken);
}
}

return buf.toString();
}

public static void main(String[] args) {
new paren();
}
}


Follow the logic when broken == '(' after a space.
 
K

Kabal

public class ProperCase {
private ProperCase() {
}

public static String convert(String s) {
char[] chars = s.toLowerCase().trim().toCharArray();
boolean found = false;

for (int i=0; i<chars.length; i++) {
if (!found && chars >= 'a' && chars <= 'z') {
chars = String.valueOf(chars).toUpperCase().charAt(0);
found = true;
} else if (chars==' ') {
found = false;
}
}

return String.valueOf(chars);
}
}

class TestProperCase {
public static void main(String[] args) {
String str = "HI THERE BUDDY!";
System.out.println("Before proper case:\t" + str);
System.out.println("After proper case:\t" + ProperCase.convert(str));
}
}
 
P

Prakash Prabhu

(e-mail address removed) wrote:

Actually, the problem with the code you posted is that when you
encounter a whitespace you set the doUpperCase to true and during the
next iteration of the loop,you convert the next char (the '(')
to upper case and also set doUpperCase to false.So for the next char
(the one after '(') , doUpperCase is false and so behaviour.

Try this :
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 (String.valueOf(broken).equals("(") || broken == ' ')
{
doUpperCase = true;
buf.append(broken);
}
else if (doUpperCase) {
buf.append(String.valueOf(broken).toUpperCase());
doUpperCase = false;
}
else buf.append(broken);

}
return buf.toString();
}

public static void main(String[] args) {
new paren();
}
}

Regards,
Prakash
 
K

Kabal

// Updated version from my last post. Forgot about character wrapper class

public class ProperCase {
private ProperCase() {
}

public static String convert(String s) {
char[] chars = s.trim().toLowerCase().toCharArray();
boolean found = false;

for (int i=0; i<chars.length; i++) {
if (!found && Character.isLetter(chars)) {
chars = Character.toUpperCase(chars);
found = true;
} else if (Character.isWhitespace(chars)) {
found = false;
}
}

return String.valueOf(chars);
}
}

class TestProperCase {
public static void main(String[] args) {
String str = "STRING THAT (HAS PARENS) INSIDE OF IT.";
System.out.println("Before proper case:\t" + str);
System.out.println("After proper case:\t" + ProperCase.convert(str));
}
}
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top