I'm using BufferedReader to read a string, which is converted into a character array.

My program works find with short strings, but not long ones?

Is there a limit to the char array's?

Thanks in advance!

Recommended Answers

All 6 Replies

Well, they're limited to 2,147,483,647 elements (which would be 4gb of data). I doubt you are hitting that limit.

What do you think could be the problem?

Well, based on all the code you posted and the detailed explanation of the error ...

commented: hahahahaha I needed that laugh! +7

Sorry! I was being lazy and hoped for some quick fixes to this possibly common issue.

Here's the body of it. It's suppose to print text to a certain amount of spaces, the text lined up to the left.

int SPACES = 20;				
int SPACE_CHAR = 32;					
int leftEdge = 0;						
int rightEdge = SPACES;

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String text;							// read as string
text = input.readLine();
char[] charText = text.toCharArray();	// convert to character array
System.out.println();
		

while (charText[leftEdge] == SPACE_CHAR) {
	++leftEdge;
}

if (rightEdge >= charText.length) {
	rightEdge = charText.length - 1;
		
	for (int i = leftEdge ; i <= rightEdge ; i++) {
				System.out.print(charText[i]);
	}
}
else	for (int i = 0 ; i < charText.length ; i++) {

	while (charText[rightEdge] != SPACE_CHAR) {
		--rightEdge;
	}
			
	for (int j = leftEdge ; j < rightEdge ; j++) {
		System.out.print(charText[j]);
	}
	System.out.println();
			
	leftEdge = rightEdge + 1;
	rightEdge = leftEdge + SPACES;
			
	if (rightEdge >= charText.length) {
		rightEdge = charText.length - 1;
				
		for (int j = leftEdge ; j <= rightEdge ; j++) {
			System.out.print(charText[j]);
		}
				
		i = charText.length;
	} // if
} // main for loop
System.out.println();

I didn't run into problems with long string (at least up to 3662 characters - I didn't try more) as long as the longest unbroken string of characters (single word) was less than <your guess here> . Here's a hint.

while (charText[rightEdge] != SPACE_CHAR) {
                    --rightEdge;
                }

Trace through what happens after you have moved 'rightEdge' with this line rightEdge = leftEdge + SPACES; and you encounter a long unbroken string of characters.

Ok, I have to head out for the evening, so just in case my hints are too vague I don't want to leave you scratching your head all weekend.

If your loop encounters a word longer than SPACES , take a look at where 'leftEdge' and 'rightEdge' end up and what that does to the rest of your processing loop. You'll have to decide how you want to break the line if that occurs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.