Java:.charAt无法识别空格字符

Java:.charAt无法识别空格字符,java,string,charat,Java,String,Charat,我不想在这里麻烦,但我的一个个人项目一直在返回一个顽固的错误。因为我是计算机科学专业的学生,所以我让几个同学和我的教授都看了一遍,但都没有用 问题是,我正试图编写一个程序,它接受一个字符串,并且每40个字符将该段存储到数组中的一个点中,因此最终的print语句只需要在新行上打印出数组中的每个元素,整个文本长度将保持在40个字符以内 一旦我修改了这个程序,错误就出现了,所以如果字符串中的第40个字符是一个字母,那么程序就会知道它正在删除一个单词。因此,为了与此相反,我从那个点向后搜索到最后一个索引

我不想在这里麻烦,但我的一个个人项目一直在返回一个顽固的错误。因为我是计算机科学专业的学生,所以我让几个同学和我的教授都看了一遍,但都没有用

问题是,我正试图编写一个程序,它接受一个字符串,并且每40个字符将该段存储到数组中的一个点中,因此最终的print语句只需要在新行上打印出数组中的每个元素,整个文本长度将保持在40个字符以内

一旦我修改了这个程序,错误就出现了,所以如果字符串中的第40个字符是一个字母,那么程序就会知道它正在删除一个单词。因此,为了与此相反,我从那个点向后搜索到最后一个索引,它是一个空格,并在那里截线。剩下的单词被添加到新行中,程序继续运行

然而,情况并非如此。无论出于何种原因,它在返回字符串时都找不到空格字符,尽管有许多空格字符

以下是问题产生的特定部分:

//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]);
    }

}
}
import java.util.*;
公共类错误检查{
公共静态void main(字符串[]args){
//原句
String SEQUANCE=“此测试句子应在每40个字符处正确截断,并在下一行添加任何截断的单词。”;
//数组来存储每一行
ArrayList words=新的ArrayList();
//正在填充的当前行
字符串换行=”;
//从语句字符串中读取的单个字符
字符='';
//字符串,以保留创建新行时断开的任何单词
字符串cutWord=“”;
//int以跟踪要移动的索引数
int cutAdd=0;
//char以跟踪在行尾被截断的单词中的字符
char cutChar='';
//int,用于在搜索被剪切单词的开头时跟踪临时位置
内部温度=0;
for(int i=0;i<句子长度();i++){
//在句子中逐一检查字符
字符=句子。字符(i);
//确保每条线的最大长度为40个字符
如果(i%40==0&&i>1){
//如果40标记处的字符是空格或昏迷,请将其添加到行中并开始新行
如果(字符=“”| |字符=“”,’){
换行符+=字符;
添加(换行符);
新行=”;
}
/(否则,行在单词的中间停止)
否则{
//将临时字符和索引设置为当前字符和索引
cutChar=字符;
温度=i;
//下面的while循环搜索字符串中的最后一个索引,该索引是一个空格,因此找到了被剪切单词的开头。另外,还考虑了索引到达字符串开头的可能性
while(cutChar!=''&&temp>0){
温度--;
cutChar=句子.charAt(temp);
//减去要添加到下一行的字符行
newLine=newLine.substring(0,newLine.length()-1);
}
//一旦找到空格且while循环中断,添加一个索引以开始完全读取断开的单词
temp++;
cutWord=“”;
//这个while循环确保读取,直到它遇到另一个空格或到达字符串的末尾(即使这个剪切字恰好是最后一个字)
while(cutChar!=''&&Session.length()>=temp){
//检查句子中的字符,将其添加到切字中,并增加索引
cutChar=句子.charAt(i);
cutWord+=cutChar;
temp++;
如果(温度>=40){
//统计恢复时要添加到正常索引的其他索引
cutAdd++;
}
}
//退出循环后,字符串“cutWord”应该是两行之间的完整单词
//添加新行(减去剪切字的字符)
添加(换行符);
//以cutWord作为开始,开始新行
换行符+=切字;
//按新字符的数量增加索引
i+=cutAdd;
//重置切割变量
cutWord=“”;
cutAdd=0;
} 
}
//此循环确保始终添加最后一个字符
else if(i==(句子长度()-1)){
换行符+=字符;
添加(换行符);
}
//如果不满足其他条件,则将当前字符简单地添加到换行符中
否则{
换行符+=字符;
}
}
//在所有这些之后,应该有一个arraylist,每个元素都有一行
String[]wordsArray=新字符串[words.size()];
//将该arraylist转换为常规的arraylist(因为理想情况下,它最终会被发送到其他地方)
wordsArray=words.toArray(wordsArray);
//应该以40个字符或更少的行打印句子
for(int i=0;i
当前,while循环无法在字符串中的空格字符处停止,输出如下所示:


有人知道这个问题的解决方法吗?

我认为检查字符是空格还是逗号的if语句实际上应该是while循环。继续减小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;
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.
String wrapped = WordUtils.wrap(theText, 40, “~~~”, true); 
String[] split = wrapped.split(“~~~”);