The Artima Developer Community
Sponsored Link

Java Answers Forum
String breaking problem

6 replies on 1 page. Most recent reply: Mar 14, 2003 9:46 AM by M

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
M

Posts: 4
Nickname: b81
Registered: Jan, 2003

String breaking problem Posted: Mar 12, 2003 7:44 AM
Reply to this message Reply
Advertisement
Hi. I have made a program that wraps the text into a spesific length, but there is a small problem that I cant solve. Why does the program exceed the length when the next String is exactly the same length as the remaining line. Here is the code:
import java.util.*;
 
public class StringTraining {
	public static void main ( String[] args ) {
		
		StringTraining Test = new StringTraining ();
		
		String text = "This is the text that should be manipulated correctly"+
		" but there is a problem with it! I have tried to figure it out for"+
		" several hours. When the string is the same size as the remaining"+
		" space there is an error Please help, this is driving me crazy";
		
		text = Test.Manipulate ( text, 30 );
		System.out.println ( text );
	
	} // main
	
	public String Manipulate ( String text, int length ) {
		
		StringBuffer sb = new StringBuffer (  ); 
		StringTokenizer st = new StringTokenizer ( text );
		int temp = length;
		
		while ( st.hasMoreTokens() ) { 	
			String next_string = st.nextToken();  
			int srting_length = next_string.length();  
			
			if ( srting_length < temp ) { 
				sb.append ( next_string+" ");    
				temp = ( (temp - srting_length) -1);
			} //if							  
				
			else if ( srting_length > temp ) { 		
			  char [] target = next_string.toCharArray();
			     for ( int n = 0; n < srting_length; n++ ) {
				sb.append(target[n]);
				--temp;
					
				    if (temp <= 0 ) {
				       n++;
					sb.append("\n"+target[n]);
					temp = ((length)-1);
								
				    }//if
					
				
						
								
			    }//for
			sb.append(" ");
			temp--;
			}//else if
		
		
		
	    } //while	
		
		return sb.toString(); 
		
	} //Manipulate
 
 
} //StringTraining
	
	
[/java]


Jay Feng

Posts: 7
Nickname: mcsquared
Registered: Mar, 2003

Re: String breaking problem Posted: Mar 13, 2003 9:10 AM
Reply to this message Reply
how about change
"else if ( srting_length > temp ) { "
to
"else if ( srting_length >= temp ) { "
?

Just a guess. I didn't test it.

Jay Feng

Posts: 7
Nickname: mcsquared
Registered: Mar, 2003

Re: String breaking problem Posted: Mar 13, 2003 9:11 AM
Reply to this message Reply
or even simpler,
just change the line to "else {" .

M

Posts: 4
Nickname: b81
Registered: Jan, 2003

Re: String breaking problem Posted: Mar 13, 2003 10:05 AM
Reply to this message Reply
I tried but still there are lines that exceed the line length. The temp goes to negative values somewhere, but I can not find the place.

Fobo

Posts: 4
Nickname: ossi
Registered: Mar, 2003

Re: String breaking problem Posted: Mar 13, 2003 11:24 AM
Reply to this message Reply
Try to solve your problem with XP-programming! ;)

igor

Posts: 1
Nickname: garry
Registered: Mar, 2003

Re: String breaking problem Posted: Mar 14, 2003 9:11 AM
Reply to this message Reply
Here you go:

public String Manipulate ( String text, int length ) {

StringBuffer sb = new StringBuffer ( );
StringTokenizer st = new StringTokenizer ( text );
int temp = length;

while ( st.hasMoreTokens() ) {
String next_string = st.nextToken();
int srting_length = next_string.length();
if ( srting_length < temp ) {
sb.append ( next_string+" ");
temp = ( (temp - srting_length)-1);
} //if
else if(srting_length == temp) {
sb.append ( next_string+"\n");
temp = length;//( (temp - srting_length));

}
else if ( srting_length > temp ) {
char [] target = next_string.toCharArray();
for ( int n = 0; n < srting_length; n++ ) {

if (temp <= 0 ) {

sb.append("\n"+target[n]);
temp = length-1;

}else {
sb.append(target[n]);
temp--;

}




}//for
sb.append(" ");
temp--;
}//else if



} //while

return sb.toString();

} //Manipulate

M

Posts: 4
Nickname: b81
Registered: Jan, 2003

Re: String breaking problem Posted: Mar 14, 2003 9:46 AM
Reply to this message Reply
Thank you very much :)

Flat View: This topic has 6 replies on 1 page
Topic: get an element of an ArrayList Previous Topic   Next Topic Topic: WHICH ONE IS FASTER

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use