I hate to bother on here, but a personal project of mine is persistently returning a stubborn error. Since I'm a computer science major, I've had a few fellow students look over it as well as my professor, to no avail.
The issue is I'm trying to write a program that takes a String, and at every 40 characters stores that segment into a spot in an array, so that the final print statement only needs to print out each element in the array on a new line and the whole text length will stay within 40 characters.
The errors arose once I modified said program, so that if the 40th character in the string was a letter, then the program will know that it is cutting off a word. Thus, to contravene this, I searched backward from that spot to the last index that was a space, and cut the line there instead. The remaining word is then added to the new line, and the program continues on.
However, that is not the case. For whichever reason, it cannot find a space character when searching back through the String, despite many being there.
Here's the particular segment the issue arises from:
//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);
}
And here is the program in it's entirity, comment-coded to the high heavens:
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]);
}
}
}
Currently, the while loop fails to stop at a space character in the String, and the output looks like this:
Anyone knows a fix for this?