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