I'm glad I found this forum. Just doing seraches has helped me already. However, I am having a problem with my assigment.

I want to open data.txt file, read the file line by line. Each line has four items, int, string, int, and double. Then put the items into an array, and then print out the items in the array.

At this time, the code reads one line only.

Here is what is in my data.txt file:
3176 battery 15 45.25
2217 tire 10 12.50

Here is my code. Any help is appreciated,
Jim

iimport java.io.*;
import java.util.*;
		  		 		  
public class ReadLines{

	public static void main(String args[]) {
				
		int count = 0;
		int MAX_LENGTH = 3;
		int partID = 0;
		String partName = " ";
		int partStock = 0;
		double partPrice = 0;
		
  		try{
   		BufferedReader in = 
    		new BufferedReader(new FileReader ("data.txt"));// read file
			CarPart[] array = new CarPart[MAX_LENGTH];//array
				
			String line = in.readLine();//read a line in file 
         StringTokenizer st = new StringTokenizer(line);//tokenizer
			
				if (line != null) { 		
							 						 						  
					partID = Integer.parseInt(st.nextToken());
   				partName = st.nextToken();
   				partStock = Integer.parseInt(st.nextToken());
   				partPrice = Double.parseDouble(st.nextToken());
				
					CarPart cp = new CarPart(partID, partName, partStock, partPrice);
			
					array[count] = cp;
					count++;
				}//end if
			
		System.out.println("Part: " + partID +" " +partName + " " + partStock + " " + partPrice);
										 
	    in.close();
       }//end try
			 
           catch (Exception e) {      
            System.err.println(e); // Print the exception to warn.
          		 }//end catch										 
         }//end main
}//end class

Hey jim,


You need a loop to make it repeat( for, while , do ...while etc..)
this line ----> if (line != null) { should be

while ( line != null )
{
....
...

....
line = in.readline(); // last thing u should do to get to next line, it will exit if //there are no more lines, hope this helps
}

Good luck,
Mel

If you want to get each item seperate, you will need to use a StringTokenizer.

While looping, you could take the read line and tokenize it:

ArrayList first = new ArrayList();
ArrayList second = new ArrayList();
ArrayList third = new ArrayList();
ArrayList fourth = new ArrayList();
int count = 1;
while (br.readLine() != null)
{

        StringTokenizer st = new StringTokenizer(br.readLine());
        while (st.hasMoreTokens())
       {
          if (count == 1)
         {
                 first.add(st.nextToken());
         }
         else if (count == 2)
        {
               second.add(st.nextToken();
         }
      }
}

You get the drift.

Thanks for the help. I finally got the program working. I posted the final code below.

Thanks again,
Jim

public static void main(String args[])throws IOException  {
	
	BufferedReader fileIn = new BufferedReader(new InputStreamReader (System.in));
	
		int count = 0;
		int MAX_LENGTH = 4;
		int partID = 0;
		String partName = " ";
		int partStock = 0;
		double partPrice = 0;
		CarPart [] array = new CarPart[MAX_LENGTH];//array

		String fileName;
		System.out.print("Enter file name: ");
		fileName = fileIn.readLine();

		try {
 			 BufferedReader in = new BufferedReader(new FileReader(fileName));
  				String line;

  					while((line = in.readLine()) != null) {  // Read line, check for end-of-file
  					 System.out.println("From File: " +line);              // Print the line
 
					  		StringTokenizer st = new StringTokenizer(line);//tokenizer
							partID = Integer.parseInt(st.nextToken());
    						partName = st.nextToken();
    						partStock = Integer.parseInt(st.nextToken());
    						partPrice = Double.parseDouble(st.nextToken());
					  
					  CarPart cp = new CarPart(partID, partName, partStock, partPrice);
					 								  						
							 if (count < MAX_LENGTH){
							 array[count]=cp;
							 count++;
							 
							 	}//end if
//line = in.readLine();
 					 }//end while
  					in.close();    // Always close a stream when you are done with it
				
		}//end try
		catch (IOException e) {
  		// Handle FileNotFoundException, etc. here
		}//end catch
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.