Java: .charAt 无法识别 space 字符
Java: .charAt not recognizing space character
我不想在这里打扰,但我的个人项目一直在返回一个顽固的错误。因为我是计算机科学专业的,所以我有几个同学和我的教授看过它,但无济于事。
问题是我正在尝试编写一个接受字符串的程序,每 40 个字符将该段存储到数组中的一个位置,以便最终的打印语句只需要打印出每个元素数组换行,全文长度控制在40个字符以内。
我修改了那个程序就出现了这个错误,所以如果字符串中的第40个字符是一个字母,那么程序就会知道它正在截断一个单词。因此,为了违背这一点,我从那个地方向后搜索到最后一个索引 space,然后在那里截断该行。然后将剩余的单词添加到新行,程序继续。
然而,事实并非如此。无论出于何种原因,它在搜索字符串时都找不到 space 字符,尽管那里有很多字符。
这是出现问题的特定部分:
//the following while loop searches for the last index in the string that
// was a space, therefore finding the beginning of the cut word.
//Also account for the chance the index reaches the start of the string
while(cutChar != ' ' && temp > 0){
temp--;
cutChar = sentence.charAt(temp);
//minuses the line of the char to be added to the next line
newLine = newLine.substring(0, newLine.length() - 1);
}
这是完整的程序,注释编码到高天:
import java.util.*;
public class errorCheck{
public static void main (String [] args) {
//original sentence
String sentence = "This test sentence should cut off properly at every 40th character and add any cut words to the next line.";
//array to store each line
ArrayList <String> words = new ArrayList<String>();
//current line being filled
String newLine = "";
//individual character being read from the sentance string
char character = ' ';
//string to preserve any word severed when a new line is created
String cutWord = "";
//int to keep track of how many indexes to move over
int cutAdd = 0;
//char to keep track of the chars in the word being cut off at the end of the line
char cutChar = ' ';
//int to keep track of temporary place when searching for the beginning of the cut word
int temp = 0;
for (int i = 0; i < sentence.length(); i++){
//examines the chars one by one in the sentance
character = sentence.charAt(i);
//makes sure each line is max 40 chars long
if(i%40 == 0 && i > 1){
//if the char at the 40 mark is a space or coma, add it to the line and start a new line
if (character == ' ' || character == ','){
newLine += character;
words.add(newLine);
newLine = "";
}
//else (aka the line stops in the middle of a word)
else{
//sets temporary character and index to current one
cutChar = character;
temp = i;
//the following while loop searches for the last index in the string that was a space, therefore finding the beginning of the cut word. Also account for chance the index reaches the start of the string
while(cutChar != ' ' && temp > 0){
temp--;
cutChar = sentence.charAt(temp);
//minuses the line of the char to be added to the next line
newLine = newLine.substring(0, newLine.length() - 1);
}
//once a space was found and the while loop broken, add a index to begin reading the severed word completely
temp++;
cutWord = "";
//this while loop makes sure to read until it comes across another space or reaches the end of the string (in the even that this cut word happens to be the final word)
while(cutChar != ' ' && sentence.length() >= temp){
//examines the chars in the sentance, adds it to the cut word, and increases the index
cutChar = sentence.charAt(i);
cutWord += cutChar;
temp++;
if (temp >= 40){
//counts the additional indexes to be added to the normal index when resumed
cutAdd++;
}
}
//after exiting the loop, the string "cutWord" should be the full word cut between the two lines
//adds the new line (minus the chars taken for the cut word)
words.add(newLine);
//starts a new line with cutWord being the start
newLine += cutWord;
//increases index by amount of new characters
i += cutAdd;
//resets the cut variables
cutWord = "";
cutAdd = 0;
}
}
//This loop makes sure that the final char is always added
else if (i == (sentence.length() - 1)){
newLine += character;
words.add(newLine);
}
//if no other condition is met, the current character is simplily added to newLine
else{
newLine += character;
}
}
//after all that, there should be an arraylist with a line for each element
String[] wordsArray = new String[words.size()];
//turn that arraylist to a regular one (because ideally in the end it's going to be sent over somewhere else)
wordsArray = words.toArray(wordsArray);
//should print out the sentance in lines that are 40 chars or less
for (int i = 0; i < wordsArray.length; i++){
System.out.println(wordsArray[i]);
}
}
}
目前,while 循环无法在字符串中的 space 字符处停止,输出如下所示:
有人知道解决这个问题的方法吗?
我认为检查字符是 space 还是逗号的 if 语句实际上应该是一个 while 循环。并在 char 再次为 space 时继续减少 i。
if (character != ' ' && character != ','){
i--;
character = sequence.charAt(i);
while(character != ' ' && character != ','){
i--;
character = sequence.charAt(i);
}
}
newLine += character;
words.add(newLine);
newLine = "";
并且你去掉了 else 子句。现在无法测试它所以告诉我它有效。
所以首先你不需要很多代码。我完全删除了以下部分。我还删除了临时变量。
//once a space was found and the while loop broken, add a index to begin reading the severed word completely
temp++;
cutWord = "";
//this while loop makes sure to read until it comes across another space or reaches the end of the string (in the even that this cut word happens to be the final word)
while(cutChar != ' ' && sentence.length() >= temp){
//examines the chars in the sentance, adds it to the cut word, and increases the index
cutChar = sentence.charAt(i);
cutWord += cutChar;
temp++;
if (temp >= 40){
//counts the additional indexes to be added to the normal index when resumed
cutAdd++;
}
}
//after exiting the loop, the string "cutWord" should be the full word cut between the two lines
//adds the new line (minus the chars taken for the cut word)
words.add(newLine);
//starts a new line with cutWord being the start
newLine += cutWord;
//increases index by amount of new characters
i += cutAdd;
//resets the cut variables
cutWord = "";
cutAdd = 0;
此外,您的问题在于您如何跟踪阅读句子的位置。如果你向后移动几个空格,那么它现在 <40 所以会在下一行过早地重新触发,这会导致奇怪的行为。因此,我添加了一个名为 readChars 的单独变量。问题已解决,以下代码现在可以使用了。
import java.util.*;
public class errorCheck{
public static void main (String [] args) {
//original sentence
String sentence = "This test sentence should cut off properly at every 40th character and add any cut words to the next line.";
//array to store each line
ArrayList <String> words = new ArrayList<String>();
//current line being filled
String newLine = "";
//individual character being read from the sentance string
char character = ' ';
//string to preserve any word severed when a new line is created
String cutWord = "";
//int to keep track of how many indexes to move over
int cutAdd = 0;
//char to keep track of the chars in the word being cut off at the end of the line
char cutChar = ' ';
int charsRead = -1;
for (int i = 0; i < sentence.length(); i++){
charsRead++;
//examines the chars one by one in the sentance
character = sentence.charAt(i);
//makes sure each line is max 40 chars long
if(charsRead >= 40 && i > 1){
//if the char at the 40 mark is a space or coma, add it to the line and start a new line
if (character == ' ' || character == ','){
newLine += character;
words.add(newLine);
newLine = "";
}
//else (aka the line stops in the middle of a word)
else{
//sets temporary character and index to current one
cutChar = character;
//the following while loop searches for the last index in the string that was a space, therefore finding the beginning of the cut word. Also account for chance the index reaches the start of the string
while(cutChar != ' ' && i > 0){
i--;
cutChar = sentence.charAt(i);
//minuses the line of the char to be added to the next line
newLine = newLine.substring(0, newLine.length() - 1);
}
//after exiting the loop, the string "cutWord" should be the full word cut between the two lines
//adds the new line (minus the chars taken for the cut word)
words.add(newLine);
newLine = "";
}
charsRead = 0;
}
//This loop makes sure that the final char is always added
else if (i == (sentence.length() - 1)){
newLine += character;
words.add(newLine);
}
//if no other condition is met, the current character is simplily added to newLine
else{
newLine += character;
}
}
//after all that, there should be an arraylist with a line for each element
String[] wordsArray = new String[words.size()];
//turn that arraylist to a regular one (because ideally in the end it's going to be sent over somewhere else)
wordsArray = words.toArray(wordsArray);
//should print out the sentance in lines that are 40 chars or less
for (int i = 0; i < wordsArray.length; i++){
System.out.println(wordsArray[i]);
}
}
}
以上代码产生以下输出。
This test sentence should cut off
properly at every 40th character and
add any cut words to the next line.
为了完成这项任务,需要重新编写大量代码。为什么不使用这样的东西(WordUtils 来自 Apache Commons Text):
String wrapped = WordUtils.wrap(theText, 40, “~~~”, true);
String[] split = wrapped.split(“~~~”);
我不想在这里打扰,但我的个人项目一直在返回一个顽固的错误。因为我是计算机科学专业的,所以我有几个同学和我的教授看过它,但无济于事。
问题是我正在尝试编写一个接受字符串的程序,每 40 个字符将该段存储到数组中的一个位置,以便最终的打印语句只需要打印出每个元素数组换行,全文长度控制在40个字符以内。
我修改了那个程序就出现了这个错误,所以如果字符串中的第40个字符是一个字母,那么程序就会知道它正在截断一个单词。因此,为了违背这一点,我从那个地方向后搜索到最后一个索引 space,然后在那里截断该行。然后将剩余的单词添加到新行,程序继续。
然而,事实并非如此。无论出于何种原因,它在搜索字符串时都找不到 space 字符,尽管那里有很多字符。
这是出现问题的特定部分:
//the following while loop searches for the last index in the string that
// was a space, therefore finding the beginning of the cut word.
//Also account for the chance the index reaches the start of the string
while(cutChar != ' ' && temp > 0){
temp--;
cutChar = sentence.charAt(temp);
//minuses the line of the char to be added to the next line
newLine = newLine.substring(0, newLine.length() - 1);
}
这是完整的程序,注释编码到高天:
import java.util.*;
public class errorCheck{
public static void main (String [] args) {
//original sentence
String sentence = "This test sentence should cut off properly at every 40th character and add any cut words to the next line.";
//array to store each line
ArrayList <String> words = new ArrayList<String>();
//current line being filled
String newLine = "";
//individual character being read from the sentance string
char character = ' ';
//string to preserve any word severed when a new line is created
String cutWord = "";
//int to keep track of how many indexes to move over
int cutAdd = 0;
//char to keep track of the chars in the word being cut off at the end of the line
char cutChar = ' ';
//int to keep track of temporary place when searching for the beginning of the cut word
int temp = 0;
for (int i = 0; i < sentence.length(); i++){
//examines the chars one by one in the sentance
character = sentence.charAt(i);
//makes sure each line is max 40 chars long
if(i%40 == 0 && i > 1){
//if the char at the 40 mark is a space or coma, add it to the line and start a new line
if (character == ' ' || character == ','){
newLine += character;
words.add(newLine);
newLine = "";
}
//else (aka the line stops in the middle of a word)
else{
//sets temporary character and index to current one
cutChar = character;
temp = i;
//the following while loop searches for the last index in the string that was a space, therefore finding the beginning of the cut word. Also account for chance the index reaches the start of the string
while(cutChar != ' ' && temp > 0){
temp--;
cutChar = sentence.charAt(temp);
//minuses the line of the char to be added to the next line
newLine = newLine.substring(0, newLine.length() - 1);
}
//once a space was found and the while loop broken, add a index to begin reading the severed word completely
temp++;
cutWord = "";
//this while loop makes sure to read until it comes across another space or reaches the end of the string (in the even that this cut word happens to be the final word)
while(cutChar != ' ' && sentence.length() >= temp){
//examines the chars in the sentance, adds it to the cut word, and increases the index
cutChar = sentence.charAt(i);
cutWord += cutChar;
temp++;
if (temp >= 40){
//counts the additional indexes to be added to the normal index when resumed
cutAdd++;
}
}
//after exiting the loop, the string "cutWord" should be the full word cut between the two lines
//adds the new line (minus the chars taken for the cut word)
words.add(newLine);
//starts a new line with cutWord being the start
newLine += cutWord;
//increases index by amount of new characters
i += cutAdd;
//resets the cut variables
cutWord = "";
cutAdd = 0;
}
}
//This loop makes sure that the final char is always added
else if (i == (sentence.length() - 1)){
newLine += character;
words.add(newLine);
}
//if no other condition is met, the current character is simplily added to newLine
else{
newLine += character;
}
}
//after all that, there should be an arraylist with a line for each element
String[] wordsArray = new String[words.size()];
//turn that arraylist to a regular one (because ideally in the end it's going to be sent over somewhere else)
wordsArray = words.toArray(wordsArray);
//should print out the sentance in lines that are 40 chars or less
for (int i = 0; i < wordsArray.length; i++){
System.out.println(wordsArray[i]);
}
}
}
目前,while 循环无法在字符串中的 space 字符处停止,输出如下所示:
有人知道解决这个问题的方法吗?
我认为检查字符是 space 还是逗号的 if 语句实际上应该是一个 while 循环。并在 char 再次为 space 时继续减少 i。
if (character != ' ' && character != ','){
i--;
character = sequence.charAt(i);
while(character != ' ' && character != ','){
i--;
character = sequence.charAt(i);
}
}
newLine += character;
words.add(newLine);
newLine = "";
并且你去掉了 else 子句。现在无法测试它所以告诉我它有效。
所以首先你不需要很多代码。我完全删除了以下部分。我还删除了临时变量。
//once a space was found and the while loop broken, add a index to begin reading the severed word completely
temp++;
cutWord = "";
//this while loop makes sure to read until it comes across another space or reaches the end of the string (in the even that this cut word happens to be the final word)
while(cutChar != ' ' && sentence.length() >= temp){
//examines the chars in the sentance, adds it to the cut word, and increases the index
cutChar = sentence.charAt(i);
cutWord += cutChar;
temp++;
if (temp >= 40){
//counts the additional indexes to be added to the normal index when resumed
cutAdd++;
}
}
//after exiting the loop, the string "cutWord" should be the full word cut between the two lines
//adds the new line (minus the chars taken for the cut word)
words.add(newLine);
//starts a new line with cutWord being the start
newLine += cutWord;
//increases index by amount of new characters
i += cutAdd;
//resets the cut variables
cutWord = "";
cutAdd = 0;
此外,您的问题在于您如何跟踪阅读句子的位置。如果你向后移动几个空格,那么它现在 <40 所以会在下一行过早地重新触发,这会导致奇怪的行为。因此,我添加了一个名为 readChars 的单独变量。问题已解决,以下代码现在可以使用了。
import java.util.*;
public class errorCheck{
public static void main (String [] args) {
//original sentence
String sentence = "This test sentence should cut off properly at every 40th character and add any cut words to the next line.";
//array to store each line
ArrayList <String> words = new ArrayList<String>();
//current line being filled
String newLine = "";
//individual character being read from the sentance string
char character = ' ';
//string to preserve any word severed when a new line is created
String cutWord = "";
//int to keep track of how many indexes to move over
int cutAdd = 0;
//char to keep track of the chars in the word being cut off at the end of the line
char cutChar = ' ';
int charsRead = -1;
for (int i = 0; i < sentence.length(); i++){
charsRead++;
//examines the chars one by one in the sentance
character = sentence.charAt(i);
//makes sure each line is max 40 chars long
if(charsRead >= 40 && i > 1){
//if the char at the 40 mark is a space or coma, add it to the line and start a new line
if (character == ' ' || character == ','){
newLine += character;
words.add(newLine);
newLine = "";
}
//else (aka the line stops in the middle of a word)
else{
//sets temporary character and index to current one
cutChar = character;
//the following while loop searches for the last index in the string that was a space, therefore finding the beginning of the cut word. Also account for chance the index reaches the start of the string
while(cutChar != ' ' && i > 0){
i--;
cutChar = sentence.charAt(i);
//minuses the line of the char to be added to the next line
newLine = newLine.substring(0, newLine.length() - 1);
}
//after exiting the loop, the string "cutWord" should be the full word cut between the two lines
//adds the new line (minus the chars taken for the cut word)
words.add(newLine);
newLine = "";
}
charsRead = 0;
}
//This loop makes sure that the final char is always added
else if (i == (sentence.length() - 1)){
newLine += character;
words.add(newLine);
}
//if no other condition is met, the current character is simplily added to newLine
else{
newLine += character;
}
}
//after all that, there should be an arraylist with a line for each element
String[] wordsArray = new String[words.size()];
//turn that arraylist to a regular one (because ideally in the end it's going to be sent over somewhere else)
wordsArray = words.toArray(wordsArray);
//should print out the sentance in lines that are 40 chars or less
for (int i = 0; i < wordsArray.length; i++){
System.out.println(wordsArray[i]);
}
}
}
以上代码产生以下输出。
This test sentence should cut off
properly at every 40th character and
add any cut words to the next line.
为了完成这项任务,需要重新编写大量代码。为什么不使用这样的东西(WordUtils 来自 Apache Commons Text):
String wrapped = WordUtils.wrap(theText, 40, “~~~”, true);
String[] split = wrapped.split(“~~~”);