Must be a dumb mistake in char array loop

Discussion in 'Java' started by rcurts@dvdeastereggs.com, Aug 21, 2003.

  1. Guest

    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
     
    , Aug 21, 2003
    #1
    1. Advertising

  2. rzed Guest

    wrote:
    > 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?
    >


    Follow the logic when broken == '(' after a space.
     
    rzed, Aug 21, 2003
    #2
    1. Advertising

  3. Kabal Guest

    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));
    }
    }
     
    Kabal, Aug 22, 2003
    #3
  4. 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
     
    Prakash Prabhu, Aug 22, 2003
    #4
  5. Kabal Guest

    // 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));
    }
    }
     
    Kabal, Aug 23, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    1,062
    Ian Collins
    May 9, 2006
  2. Replies:
    3
    Views:
    738
  3. RichardOnRails
    Replies:
    2
    Views:
    530
    RichardOnRails
    Jan 29, 2012
  4. Jerry C.
    Replies:
    8
    Views:
    248
    Uri Guttman
    Nov 23, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page