Results 1 to 3 of 3
  1. #1
    Anthony5347 is offline Member
    Join Date
    Sep 2013
    Rep Power

    Default Separating strings

    So basically I have to create a java program that accepts a string and does the following: 1.) Insert a blank space before capital letters 2.) Convert any capital letters into lower case except the first letter(if it is capital). So if the user enters "HiMyNameIsJohn", the result should be "Hi my name is john". I am having problems inside my changeMe method. Everything works fine including putting a blank space in between upper case words. So from the previous example, my code would show "Hi My Name Is John". I am having issues making the code to convert the upper case letters into lower case. Im pretty sure the code should be right after the str.insert(i, " "). But I do not how to code it. Any suggestions? Ive thought about Character.toUpperCase(ch); but doesnt work.

    Java Code:
    import java.util.*;
    public class WordSeparator
       public static void main(String[] args)
          String word1, word2;
          Scanner userInput = new Scanner(;
          System.out.println("Enter a phrase: ");
          word1 = userInput.nextLine();
          word2 = changeMe(word1);
          System.out.println("Your word was: " + word2);
       public static String changeMe(String s)
          char ch;
          StringBuilder str = new StringBuilder(s);
          for(int i = 1; i < str.length(); i++)
             ch = str.charAt(i);
             if (Character.isUpperCase(ch))
                str.insert(i, " ");
          /**This statement is here to make certain
             that the string was passed correctly back 
             into the main
          return str.toString();
    Last edited by Anthony5347; 02-19-2014 at 05:20 AM.

  2. #2
    ankurt is offline Member
    Join Date
    Feb 2014
    Rep Power

    Default Re: Separating strings

    Your Character.toLowerCase(str.charAt(i)) works fine but the problem is that you are storing it in the original string after converting it into lower case. You are just simply taking that character and then doing the lower case without making changes to the string itself.

    Also your placement of "i++" is wrong in the code. It should be placed before the toLowerCase method becuase else you are converting a "blank space" to lower case.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Separating strings

    Actually, I would just leave StringBuilder uninitialized and then modify it as you iterate over the supplied String argument.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Help with separating a String to be converted to a Char
    By AnnaPatricia in forum New To Java
    Replies: 10
    Last Post: 08-16-2012, 06:49 PM
  2. separating voices in a .wav file
    By alpdog14 in forum New To Java
    Replies: 4
    Last Post: 11-29-2010, 05:42 AM
  3. Problems separating GUI from Logic
    By tyimp in forum AWT / Swing
    Replies: 1
    Last Post: 10-06-2010, 05:32 AM
  4. Separating the digits of given number
    By lb2 in forum New To Java
    Replies: 5
    Last Post: 09-09-2010, 06:29 AM
  5. Separating program logic and GUI
    By loopingman in forum AWT / Swing
    Replies: 0
    Last Post: 12-08-2008, 03:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts