gpt4 book ai didi

java - java中单词之间添加空格并使每个单词除了第一个小写之外

转载 作者:行者123 更新时间:2023-12-02 06:17:03 25 4
gpt4 key购买 nike

我会继续告诉你,是的,这是作业。然而,我在完成它时遇到了困难,迫切需要帮助。我对 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");
}

}

最佳答案

您正在附加到您正在迭代的同一字符串。相反,只需将您的句子设置为空的StringBuilder即可。然后,您可以在迭代输入时附加到该内容。例如:

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,这样就不会那么困惑了。)

关于java - java中单词之间添加空格并使每个单词除了第一个小写之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21359251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com