Method not recognizing PAREN when looping through character array

Discussion in 'Java' started by Curts, Aug 21, 2003.

  1. Curts

    Curts Guest

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

  2. Curts wrote:
    >
    > 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?


    Sure, you shouldn't be checking the doUpperCase flag before you check for '(' or ' '.
    The space before the left parenthesis in the text sets doUpperCase to true, so you do a
    toUpperCase() on the parenthesis.

    Your algorithm is not very general. Homework?

    --
    Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
    Lee Fesperman, Aug 21, 2003
    #2
    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. Josuan
    Replies:
    0
    Views:
    273
    Josuan
    Jun 2, 2008
  2. C. J. Clegg

    Whitespace before opening paren in function call?

    C. J. Clegg, Mar 7, 2009, in forum: C Programming
    Replies:
    2
    Views:
    498
    Kaz Kylheku
    Mar 7, 2009
  3. balanced paren regex's

    , Jun 17, 2006, in forum: Perl Misc
    Replies:
    9
    Views:
    124
    Xicheng Jia
    Jun 18, 2006
  4. iporter
    Replies:
    4
    Views:
    157
  5. Aaron
    Replies:
    2
    Views:
    504
    dhtml
    Apr 10, 2011
Loading...

Share This Page