1

So this is my program

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;

public class Capitalize

{
    public static void main(String [] arg)
    {

        String x = JOptionPane.showInputDialog("Enter Phase");

        String y = capsFirst(x);


        System.out.println(y);
    }


    public static String capsFirst(String str) 
    {
        String[] words = str.split(" ");

        StringBuilder ret = new StringBuilder();

        for(int i = 0; i < words.length; i++) 
        {
            ret.append(Character.toUpperCase(words[i].charAt(0)));

            ret.append(words[i].substring(1));

            if(i < words.length - 1) 
            {
                ret.append(' ');
            }
        }

        return ret.toString();
    }
}

Now the problem is i cant figure out what logic i need to make it go hey this is an number i can't edit this. and not give me this error when it gets to it:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 

String index out of range: 0

    at java.lang.String.charAt(String.java:646)
    at Capitalize.capsFirst(Capitalize.java:36)
    at Capitalize.main(Capitalize.java:25)

Example of the text i must convert is:

london (four spaces) england 2015

which must convert to

London (four spaces) England 2015

3 Answers3

2

Take a look at the contents of the array from splitting your string for the example input: it will be something like:

{"london", "", "", "", "england", "2015"}

Your problem comes when processing one of the empty strings. You can fix this by checking whether words[i] is empty before attempting to capitalize its first letter (since there is no first letter).

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Well i figured that out but I'm wondering what happens when I get a number? And what would be the best way of not modifying and transferring those non-word splits to the new string to then output? – Patrick Roanhouse Apr 01 '15 at 23:05
  • `Character.toUppercase` has no effect if the character is a digit (or otherwise has no uppercase equivalent). Just treat such "words" in the same way as any other. – Andy Turner Apr 01 '15 at 23:10
0

you could try parse the word[i] value to a number using something like described in this question

How to check if a String is numeric in Java

If it returns true for isNumber, then don't capitalise it, just append the string to ret

Community
  • 1
  • 1
Gary O' Donoghue
  • 362
  • 2
  • 4
  • 15
0

Edit: It looks like @Andy Turner gave you the answer, but here is the code that he was hinting at.

public static String capsFirst(String str) 
    {
        String[] words = str.split(" ");

        StringBuilder ret = new StringBuilder();

        for(int i = 0; i < words.length; i++) 
        {
          if (words[i].length() == 0){
            ret.append(' ');
            continue;
          }

          if (Character.isLetter(words[i].charAt(0))){
            ret.append(Character.toUpperCase(words[i].charAt(0)));
            ret.append(words[i].substring(1));
          }
          else{
            ret.append(words[i]);
          }

            if(i < words.length - 1) 
            {
                ret.append(' ');
            }
        }

        return ret.toString();
    }

Also, here is a different approach, you could do it in-place in a char array:

public class Capitalize

{
    public static void main(String [] arg)
    {

        String x = "london   england 2015";

        String y = capsFirst(x);


        System.out.println(y);
    }


    public static String capsFirst(String str) 
    {
        boolean betweenWords = false;
        char[] chars = str.toCharArray();

        for(int i = 0; i < chars.length; i++) 
        {
          if (chars[i] == ' '){
            betweenWords = true;
            continue;
          }


          if (betweenWords || i == 0){
            if (Character.isLetter(chars[i])){
              chars[i] = Character.toUpperCase(chars[i]);
            }

            betweenWords = false;
          }

        }

        return new String(chars);
    }
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137