[英]Adding Spaces between words and making every word except the first lowercase in java
我会继续让你知道是的,这是功课。 然而,我在完成它时遇到了一堵砖墙,并迫切需要帮助。 我也是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");
}
}
您将附加到您正在迭代的相同字符串。 相反,只需将您的sentence
StringBuilder
空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
,这更容易让人困惑。)
你有几个不同的问题。 主要的是你打电话的时候
sentence.setCharAt(index, Character.toLowerCase(index));
你将实际角色作为第一个参数传递而不是位置。 你看,你刚刚完成了
index = sentence.charAt(i);
所以index
就是角色本身。 Java隐式地将此字符转换为整数 - 但它不是您希望它的整数。 你应该写的
sentence.setCharAt(i, Character.toLowerCase(index));
代替。
还有,你的sentence.append(' ');
将空格附加到StringBuilder
的末尾,而不是将其插入到您想要的位置。
而你的最后sentence.append(index);
将复制角色。 我真的不认为你想这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.