3 Replies - 23768 Views - Last Post: 05 December 2007 - 04:00 AM Rate Topic: -----

#1 49wrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 07

To store strings into array of class

Posted 03 December 2007 - 08:35 PM

I have this java code with main method and class. The code is to read from an already exisiting text file and store the words found in the file into an array modified by a class. The contents of the text file are one word per line. How do I store the words in the text in an array. After that is successfully done, how do I print the contents of the array to the screen?

Here is the code, the class is down below.
import java.text.*;
import java.io.*; // needed to access input stream classes

public class  process

{
static BufferedReader keyboard =
		new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args)
		throws java.io.IOException	   
 {
Help[] list= new Help[100];

	   int count = 0;
		String input;

System.out.print("Enter text file name to read from: ");
	input = keyboard.readLine();
	holdData(input,list, count);

   }

static void holdData(String fileName, Help array[], int c) throws IOException
 {
FileReader reader = new FileReader (fileName);
BufferedReader infile = new BufferedReader (reader);

String store;
store = infile.readLine ();
array[c] = new Help(store);
while (store != null) // while not end of file
{
	store = infile.readLine();
	c++;
  array[c] = new Help(store); // is this statement going to store the words?
}
} 


the created class, merged

  class Help
{
private int count;
private String read;

public int getCount()
{
	 return count;
}
public Help (String len)
{
read = len;
count = 1;
}
	public String getHelp ()
	{
return read;
}
} 


Is This A Good Question/Topic? 0
  • +

Replies To: To store strings into array of class

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: To store strings into array of class

Posted 03 December 2007 - 08:54 PM

try:
http://www.dreaminco.../snippet414.htm
for opening and reading text files line by line.

For printing use a for loop
Was This Post Helpful? 0
  • +
  • -

#3 49wrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 07

Re: To store strings into array of class

Posted 04 December 2007 - 02:42 PM

the problem is to store strings into array of class

 array[c] = new Help(store); 


i thought this statement would do the job, but doesn't seem to. Any other ideas?
Was This Post Helpful? 0
  • +
  • -

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: To store strings into array of class

Posted 05 December 2007 - 04:00 AM

I think you should read our tutorial: Introduction to arrays
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1