954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

text processing?

Hi all,

I am trying to take a list of words (from a text file) and then split each word into characters how can I do this?

The code I currently have just reads in all the words (at, as, ate, apple, apply) found in text file and prints them out (as many times as the length of the word). What I really want is for my program to take one word from the text file and split it into characters on one line in the output. Would I have to save the words from the text file in an array first?

My code:

import java.io.*;

class FileReadTest {

public static void main (String[] args) {
FileReadTest t = new FileReadTest(); //creates object of the class
t.readMyFile(); //applies the readMyFile method to the object
}

void readMyFile() {
String record = null; //initialises the string variable record to null

try {
FileReader fr = new FileReader("C:\\Documents and Settings\\sumiya\\My Documents\\Scrabble Project\\scrabbledict.txt");
//creates a new filereader for the named file in the arguments
BufferedReader br = new BufferedReader(fr);

record = new String();
while ((record = br.readLine()) != null) {
System.out.println(record);

for(int i=0; i

nisaa15
Newbie Poster
14 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

You could use a StringTokenizer to read everything that's a word(doesn't read white space).
Get the length of the word.
In a for loop you can process the char at index 'i' by using charAt(i).

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 
String record = null;

I'd recommend changing that to String record; . Once you create a String, even if it's null, you can't change it - they're immutable. String record; will tell the app that a String called record will be created, but not actually create it until you do record = something .

As for your actual question:What I really want is for my program to take one word from the text file and split it into characters on one line in the output.
String.toCharArray(); ?

mmiikkee12
Posting Whiz in Training
274 posts since Oct 2004
Reputation Points: 17
Solved Threads: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You