GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes cutting string into peaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cutting string into peaces" Watch "cutting string into peaces" New topic
Author

cutting string into peaces

MacTosh
Greenhorn

Joined: Nov 11, 2001
Posts: 2
i have to cut string into equal length peaces according to
the length of the first word, and delete spaces between the words to one space like this
( _ = space )
"_ _ _ How__ do__ you__ do__today "
now it returns
How
__d
o__
you
__d
o__
tod
ay_
should be
How
do_
you
do_
tod
ay_
the following code cuts the string into equal length
peaces but it doesn�t cut the spaces between words so
that there is only one space that between the words.
How can i do that???
import java.io.*;

class Cutting{
public static void main(String []args)throws IOException {
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String str=" How do you do today ";
String[] cutStr;
char spr= (' ');
char rpl= (' ');
int index=0;
int offset =0;
cutStr=cut(str,spr,rpl,stdin);
for(index=0;index<str.length();index++)>
System.out.println( cutStr[index]);
//System.out.print("\n" +(cutStr=cut(str, spr, rpl))+"totanoi");
}//main
static String[] cut(String str, char spr, char rpl,BufferedReader stdin)throws IOException {
str = str.trim();
int wordLen = str.indexOf(spr);
if (wordLen == -1) {
return new String[]{ str};
}
int words = (int)Math.ceil(str.length() / (double)wordLen);
String[] cutStr = new String[words];
int offset = 0, index = 0;
for (; (offset + wordLen) < str.length(); ++index) {
cutStr[index] = str.substring(offset, (offset + wordLen)).replace(spr, rpl);
offset += wordLen;
}
cutStr[index] = str.substring(offset, str.length()).replace(spr, rpl);
while (cutStr[index].length() < wordLen) {
cutStr[index] += rpl;
}
return cutStr;
}
}//class
Rowan Brownlee
Ranch Hand

Joined: Aug 07, 2000
Posts: 97
Hi MacTosh,
This is an interesting exercise. I was thinking about how I'd do it if I was going from scratch and roughed out (possibly incomplete) ideas. You might be able to incorporate some of the following.
~~~~~
I'd look at running a loop on the string, based on it's length.
Within each iteration I'd do something like...
test each character in the string (maybe by declaring a new character based on string.charAt()).
use 1 or more of the static methods in the Character class (eg. Character.isWhitespace()) to test if the current character is a whitespace
The loop counter would help tell me when I'd finished reading the first word, and I'd use the length of the first word to determine the size of subsequent pieces.
(probably need a boolean and an if to flag when the first word had been read)
I'd be adding each character to a new stringBuffer until I'd added the same number of characters as the first word
Before printing the stringBuffer to the screen, I'd test to see if the leading character was a white space (assuming that there's only going to be one whitespace between each word).
~~~~

cheerio
rowan
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
could you do things in this order:
(a)get length of first word
(b)put string into array, one character per element
(b.1)if character is NOT a space then put character into array,
(b.2)else move on to next character
(c)loop back through array and print out number of characters equal to length of word, insert a space and so on.

if you don't know, then ask. if you do know, then share. love is knowledge.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
MacTosh
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however, your name is not in keeping with our naming policy here at the ranch. Please re-register under an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Dave Vick:
MacTosh
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however, your name is not in keeping with our naming policy here at the ranch. Please re-register under an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!

Oops sorry!!
i've re-registered under new name now!


Rowan Brownlee
Ranch Hand

Joined: Aug 07, 2000
Posts: 97
Hi Tomas,
I thought that was your real name. What's the relationship between MacTosh and Tomas Jarvi?
cheerio
rowan
 
GeeCON Prague 2014
 
subject: cutting string into peaces