在单词之间添加空格并在java中创建除第一个小写之外的每个单词

时间:2014-01-26 03:20:46

标签: java string stringbuilder

我会继续让你知道是的,这是家庭作业。然而,我在完成它时遇到了一堵砖墙,并迫切需要帮助。我也是Java的新手,我还在学习这门语言。

好的,我正在尝试编写一个程序,要求用户输入一个没有空格的句子,但要让它们大写每个单词的第一个字母。然后程序应该在单词之间添加空格,并且只有第一个单词大写,其余的应该以小写字母开头。我可以在单词之间插入空格,但我不能得到每个单词的第一个字母。我尝试了几种不同的方法,最新的方法是给我这个错误信息:

 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
 ex out of range: 72
    at java.lang.AbstractStringBuilder.setCharAt(Unknown Source)
    at java.lang.StringBuilder.setCharAt(Unknown Source)
    at renfroKristinCh9PC14.main(renfroKristinCh9PC14.java:45)

我正在发布我的代码,您可以给予我的任何和所有帮助都将非常感激。 感谢。

/*
This program will ask the user to enter a sentence without whitespaces, but 
with the first letter of each word capitilized. It will then separate the words
and have only the first word of the sentence capitalized.
*/

import java.util.*;

public class renfroKristinCh9PC14
{
public static void main(String[] args)
{
    //a string variable to hold the user's input and a variable to hold the modified sentence
    String input = "";
    //variable to hold a character
    char index;

    //create an instance of the scanner class for input
    Scanner keyboard = new Scanner(System.in);

    //welcome the user and explain the program
    userWelcome();

    //get the sentence from the user
    System.out.println("\n  Please enter a sentence without spaces but with the\n");
    System.out.println("  first letter of each word capitalized.\n");
    System.out.print("  Example: BatmanIsTheBestSuperheroEver!  ");

    input = keyboard.nextLine();

    //create an instance of the StringBuilder class
    StringBuilder sentence = new StringBuilder(input);

    //add spaces between the words
    for(int i=0; i < sentence.length(); i++)
    {
        index = sentence.charAt(i);
        if(i != 0 && Character.isUpperCase(index))
        {

            sentence.setCharAt(index, Character.toLowerCase(index));
            sentence.append(' ');


        }

        sentence.append(index);

    }   


    //show the new sentence to the user
    System.out.println("\n\n  Your sentence is now: "+sentence);
}
/***********************************************************************************    *************************
************************************************************************************    *************************

This function welcomes the user and exlains the program
*/

public static void userWelcome()
{

    System.out.println("\n\n       ****************   ****************************************************\n");
    System.out.println("  *            Welcome to the Word Seperator Program                   *");
    System.out.println("  *  This application will ask you to enter a sentence without       *");
    System.out.println("  *  spaces but with each word capitalized, and will then alter the  *");
    System.out.println("  *  sentence so that there arespaces between each word and          *");
    System.out.println("  *  only the first word of the sentence is capitalized              *");
    System.out.println("\n  ********************************************************************\n");
}

}

2 个答案:

答案 0 :(得分:2)

您将附加到您正在迭代的相同字符串。相反,只需将sentence设为空StringBuilder即可。然后,您可以在迭代input时追加到该值。例如:

StringBuilder sentence = new StringBuilder();

//add spaces between the words
for(int i=0; i < input.length(); i++)
{
  char letter = input.charAt(i);
  if(i != 0 && Character.isUpperCase(letter))
  {
    sentence.append(' ');
    sentence.append(Character.toLowerCase(letter));
  }
  else
  {
    sentence.append(letter);
  }
}   

(请注意,我已将变量名称从index更改为letter,这不会让人感到困惑。)

答案 1 :(得分:0)

这里有一些不同的问题。主要的是当你打电话时

sentence.setCharAt(index, Character.toLowerCase(index));

你将实际角色作为第一个参数传递而不是位置。你看,你刚刚完成了

index = sentence.charAt(i);

所以index是角色本身。 Java隐式地将此字符转换为整数 - 但它不是您希望它的整数。你可能应该写

sentence.setCharAt(i, Character.toLowerCase(index));

代替。

此外,您的sentence.append(' ');会将空格附加到StringBuilder的末尾,而不是将其插入到您想要的位置。

您的最终sentence.append(index);将复制该字符。我真的不认为你想这样做。