Good day to all.. I am a beginner in java. I have a problem, I don't know how to convert string into an array which reads 60 characters.. supposedly, this is my code..

import java.io.*;
import java.util.*;
 public class Strinpunct{
 	public static void main (String [] args) throws IOException {
 		BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
 		
 		String str;
 		
 		System.out.println("Enter an input: ");
 		str=br.readLine();
 		
 		String listOfDelimiters = ".,!?1234567890()'-"; 
		StringTokenizer token = new StringTokenizer(str,listOfDelimiters);
			while(token.hasMoreTokens())
				
			System.out.print(token.nextToken());
		
		
		

 	}
 	
 }

In this code, I have to separate words from punctuation marks so I used delimiters. My problem now is the length of the string. It must contain 60 characters. For example, I will input this:

A common problem when processing incoming text is to isolate
the words in the text. This is made more difficult by the
punctuation; words have commas, "quote marks",
(even brackets) next to them, or hyphens
in the middle of the word. This punctuation doesn't
count as letters when the words have to be looked up in a
# dictionary by the 12345 "**&! program.
#

the output will be..

A common problem when processing incoming text is to isolate
the words in the text This is made more difficult by the
punctuation words have commas quote marks
even brackets next to them or
hyphens
in the middle of the word This punctuation doesnt
count as letters when the words have to be looked up in a
dictionary by the program


In the program I had created, the output that will come out will just be the words "A common problem when processing incoming text is to isolate" the next words won't come out. Please teach me how to convert string into arrays of string consist of 60 characters in each line. Or Any help would be great. thank you soo much..

Recommended Answers

All 3 Replies

Well, StringTokenizer won't help. Create an array of ((string.length() / 60) + 1) then use substring and a counter (starting at 0) to populate the elements. See the API docs for String, give it a try, and post that code.

Edit: Nevermind, the problem is something completely different. String tokenizer is still not the way.

Create a List<String> and a StringBuilder, then use charAt on the original String in a loop and populate the StringBuilder until it has these "60" characters, then call toString on that placing the result into the list, then, after you're finished with the String, call toArray on the List. See the API docs for String, StringBuilder, and List/ArrayList. Give that a try, then post your code here with the "problems" you're experiencing with it.

What I would suggest 1st: Why did you started 2 threads! If you didn't get an answer at the first then will answer when you double posted. What if someone starts posting at one thread then some one else does the same at the second?

Then:
I cannot understand that 60 characters requirement? Does the input needs to be only 60 characters? Or after you get the output you need to print only up to the 60th character?

So I would like to suggest something different with masijade permission.

Don't use StringTokenizer. It is an old class. Better use the split method of the class String.

What I would do is use the replaceAll method of the String class:

import java.io.*;
import java.util.*;
 public class Strinpunct{
 	public static void main (String [] args) throws IOException {
 		BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
 		
 		String str;
 		
 		System.out.println("Enter an input: ");
 		str=br.readLine();
 		
                System.out.println(str); // for debugging

                String regex = ""; // regular expression to use
                String newStr = str.replaceAll(regex," "); // will replace everything with the empty String

                 /*
And now you have your final String
                 */
 	}
 	
 }

After you get the newStr you can print it, or use the substring method to print only the first 60 characters, or use the split method to turn that String into an array of Strings: String [] array = newStr.split(" "); Check the API in order to find out what the regular expression should be:
This link has a list of possible regular expressions:

http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html

don't forget the escape character. If you want to use the \D for example you need to input: "\\D"

It all depends on what this "60 character" bs actually means. ;-)

He doesn't state it very well.

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.