[英]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.