Discussion Board

Results 1 to 12 of 12
  1. #1
    Registered User SHENGTON's Avatar
    Join Date
    Feb 2011
    Posts
    35
    Hello, good morning.

    How to capitalize first letter of every sentence in J2ME?

    I want to capitalizes the first letter found after either a period, question mark or exclamation point.

    For example: If this is the sentence --> where are you now? please be here this afternoon.

    The output should be --> Where are you now? Please be here this afternoon.

    I tried this code:
    Code:
    temp = temp.substring(0,1).toUpperCase()+temp.substring(1);
    But the output is like this --> Where are you now? please be here this afternoon.

  2. #2
    Registered User bhanuchandar.k's Avatar
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868
    Hi SHENGTON,

    I have pust some dummy implementation of how to capitalize the first letter you can custamize this according to your needs. I assumes your input string is a lower case string.

    char[] charArr = str.toChar();
    StringBuffer result = new StringBuffer();
    for(int i = 0;i<charArr.lenght;i++)
    {
    char ch = charArr[i];
    prevCh = i==0?0:charArr[i-1];
    switch(prevCh){
    case '?':
    case '.':
    case '!':
    result.append(Character.toUpperCase(ch));
    break;
    default:
    result.append(ch);
    break;
    }
    }

  3. #3
    Registered User SHENGTON's Avatar
    Join Date
    Feb 2011
    Posts
    35
    What's the meaning of this line bhanuchandar.k?
    prevCh = i==0?0:charArr[i-1];

  4. #4
    Nokia Developer Champion danhicksbyron's Avatar
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209
    First year C: If i == 0, return 0, otherwise return the char prior to the ith char.

  5. #5
    Registered User bhanuchandar.k's Avatar
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868
    Hi SHENGTON,

    The logic is depending on the previous character . At the beginning of the loop the previous character will not be there to handle that condition I have added that.

  6. #6
    Nokia Developer Champion im2amit's Avatar
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    2,903
    This could also be done by starting the loop from 1-
    Code:
    for(int i = 1;i<charArr.lenght;i++)
    {
    char ch = charArr[i];
    prevCh = charArr[i-1];
    ....
    .
    ..
    
    .
    .
    .
    }
    thanks,
    ~Amitabh
    (Poster of the Month -Dec'12)
    Follow me on my blog for Innovative Mobile Apps

  7. #7
    Nokia Developer Moderator petrib's Avatar
    Join Date
    Mar 2003
    Posts
    9,415
    The generic/right tool for this kind of things is: regular expressions.

    http://oreilly.com/catalog/9780596528126/

  8. #8
    Registered User SHENGTON's Avatar
    Join Date
    Feb 2011
    Posts
    35
    Hello, good evening.

    @bhanuchandar.k
    Thanks for the brief explaination.

    @im2amit and petrib
    Thanks for helping me as well.

    @ to all
    I use this code to capitalize the first letter of every sentence.
    Code:
      public static String capitalize(String str)
      {
          char[] arr = str.toCharArray();
          boolean cap = true;
          boolean spacefound = true;
          for (int i = 0; i < arr.length; i++)
          {
              if (cap)
              {
                  if (arr[i] == ' ')
                  {
                      spacefound = true;
                  }
                  else
                  {
                      if (spacefound && !Character.isUpperCase(arr[i]))
                      {
                          arr[i] = Character.toUpperCase(arr[i]);
                          cap = false;
                          spacefound = false;
                      }
                  }
              }
              else
              {
                  if (arr[i] == '.' || arr[i] == '?' || arr[i] == '!')
                  {
                      cap = true;
                  }
              }
          }
          return new String(arr);
      }
    But if I will send the message twice, the result would be this --> YOu are so nice. WHere do you live? WHat is ur name?

    It should be "You are so nice. Where do you live? What is ur name?" even if I'll send it twice or more. What's lacking with the code?


    Here's my code in the send command, I just copy the part where the string should be capitalize:
    Code:
                  capital = capitalize(temp);
                  message = new TextBox ("Message", capital, 2000,TextField.ANY);

  9. #9
    Registered User SHENGTON's Avatar
    Join Date
    Feb 2011
    Posts
    35
    Ok I figured it out. Here's the perfect code:
    Code:
      public static String capitalize(String str)
      {
          char[] arr = str.toCharArray();
          boolean cap = true;
          boolean spacefound = true;
          for (int i = 0; i < arr.length; i++)
          {
              if (cap)
              {
                  if (arr[i] == ' ')
                  {
                      spacefound = true;
                  }
                  else
                  {
                      if (spacefound && !Character.isUpperCase(arr[i]))
                      {
                          arr[i] = Character.toUpperCase(arr[i]);
                      }
                      cap = false;
                      spacefound = false;
                  }
              }
              else
              {
                  if (arr[i] == '.' || arr[i] == '?' || arr[i] == '!')
                  {
                      cap = true;
                  }
              }
          }
          return new String(arr);
      }
    Thank youso much guys for helping me. You guys are great.
    Last edited by SHENGTON; 2011-02-11 at 14:08.

  10. #10
    Registered User bhanuchandar.k's Avatar
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868
    Hi SHENGTON ,

    This also works fine.
    Code:
    public static String capitalize(String str) {
    char[] arr = str.toCharArray();
    for (int i = 0; i < arr.length; i++) {
    char ch = arr[i];
    char prevChar = (i == 0 ? 0 : arr[i - 1]);
    switch (prevChar) {
    case ' ':
    case '?':
    case '.':
    case '!':
    arr[i] = Character.isUpperCase(ch) ? ch : Character.toUpperCase(ch);
    break;

    }
    }
    return new String(arr);
    }

  11. #11
    Registered User SHENGTON's Avatar
    Join Date
    Feb 2011
    Posts
    35
    Thanks bhanuchandar.k, I'll try that.

  12. #12
    Nokia Developer Champion danhicksbyron's Avatar
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209
    Here's the perfect code:
    Far from it! You also need a dictionary to recognize acronyms and proper names, capitalized the stand-alone "I", etc. Had the thing written in REXX once, many moons ago. For English there are a few simple rules that can recognize 90% of acronyms (but unfortunately would be befuddled by "fone speak").

Similar Threads

  1. J2ME: How to remove confirmation popup at every alarm ?
    By nguyenthetam in forum Mobile Java General
    Replies: 4
    Last Post: 2011-01-03, 14:53
  2. Changing the first letter to uppercase
    By nagesh.chetana in forum Mobile Java General
    Replies: 4
    Last Post: 2009-04-30, 14:06
  3. How to get Drive Letter of Self
    By kdurga in forum Symbian C++
    Replies: 3
    Last Post: 2008-10-16, 06:28
  4. How to make first application in s60 first edition?
    By kanchan sinha in forum Symbian C++
    Replies: 4
    Last Post: 2008-02-26, 13:56
  5. Some questions regaring text edit control(changing first letter to be low case)
    By alexs@servision.net in forum Symbian User Interface
    Replies: 1
    Last Post: 2007-10-02, 14:59

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved