在单词之间添加空格并使除第一个小写外的每个单词在java中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21359251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Spaces between words and making every word except the first lowercase in java
提问by Kris_R.
I'll go ahead and let you know that yes, this is homework. I have hit a brick wall in completing it however and desperately need help. I'm also pretty new to Java and am still learning the language.
我会继续让你知道是的,这是家庭作业。然而,我在完成它时遇到了砖墙,迫切需要帮助。我对 Java 也很陌生,并且仍在学习这门语言。
Okay, I am trying to write a program that asks the user to enter a sentence with no spaces but have them capitalize the first letter of each word. The program should then add spaces between the words and have only the first word capitalized, the rest should start with a lowercase. I can get the space inserted between the words, but I cannot get the first letter of each word lower-cased. I have tried several different ways, and the latest one is giving me this error message:
好的,我正在尝试编写一个程序,要求用户输入一个没有空格的句子,但让他们将每个单词的第一个字母大写。然后程序应该在单词之间添加空格,并且只有第一个单词大写,其余的应该以小写开头。我可以在单词之间插入空格,但无法将每个单词的第一个字母小写。我尝试了几种不同的方法,最新的方法是给我这个错误信息:
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)
I'm posting up my code and any and all help you can give me will be very much appreciated. Thanks.
我正在发布我的代码,非常感谢您能给我的任何帮助。谢谢。
/*
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");
}
}
}
采纳答案by chrisaycock
You are appending to the same string that you are iterating through. Instead, just make your sentence
an empty StringBuilder
. Then you can append to that while iterating through input
. For example:
您将附加到您正在迭代的同一个字符串。相反,只需将您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);
}
}
(Note that I've changed the variable name from index
to letter
, which is a lot less confusing.)
(请注意,我已将变量名称从 更改index
为letter
,这样就不那么容易混淆了。)
回答by Dawood ibn Kareem
You have a few different problems here. The main one is that when you call
你在这里有几个不同的问题。主要是当你打电话时
sentence.setCharAt(index, Character.toLowerCase(index));
you're passing in the actual character in as the first argument, instead of the position. You see, you've just done
您将实际字符作为第一个参数传入,而不是位置。你看,你刚刚完成
index = sentence.charAt(i);
so index
is the character itself. Java implicitly converts this character to an integer - but it's not the integer that you want it to be. You probably should have written
index
角色本身也是如此。Java 隐式地将此字符转换为整数 - 但它不是您想要的整数。你可能应该写
sentence.setCharAt(i, Character.toLowerCase(index));
instead.
反而。
Also, your sentence.append(' ');
will append the space to the end of the StringBuilder
, rather than inserting it where you want it to.
此外,您sentence.append(' ');
会将空格附加到 的末尾StringBuilder
,而不是将其插入到您想要的位置。
And your final sentence.append(index);
will duplicate the character. I really don't think you want to do this.
你的决赛sentence.append(index);
将复制这个角色。我真的不认为你想这样做。