Need help with Java Assignment

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2005
Posts: 2
Reputation: JOskydive is an unknown quantity at this point 
Solved Threads: 0
JOskydive JOskydive is offline Offline
Newbie Poster

Need help with Java Assignment

 
0
  #1
Jul 11th, 2005
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
  1. iimport java.io.*;
  2. import java.util.*;
  3.  
  4. public class ReadLines{
  5.  
  6. public static void main(String args[]) {
  7.  
  8. int count = 0;
  9. int MAX_LENGTH = 3;
  10. int partID = 0;
  11. String partName = " ";
  12. int partStock = 0;
  13. double partPrice = 0;
  14.  
  15. try{
  16. BufferedReader in =
  17. new BufferedReader(new FileReader ("data.txt"));// read file
  18. CarPart[] array = new CarPart[MAX_LENGTH];//array
  19.  
  20. String line = in.readLine();//read a line in file
  21. StringTokenizer st = new StringTokenizer(line);//tokenizer
  22.  
  23. if (line != null) {
  24.  
  25. partID = Integer.parseInt(st.nextToken());
  26. partName = st.nextToken();
  27. partStock = Integer.parseInt(st.nextToken());
  28. partPrice = Double.parseDouble(st.nextToken());
  29.  
  30. CarPart cp = new CarPart(partID, partName, partStock, partPrice);
  31.  
  32. array[count] = cp;
  33. count++;
  34. }//end if
  35.  
  36. System.out.println("Part: " + partID +" " +partName + " " + partStock + " " + partPrice);
  37.  
  38. in.close();
  39. }//end try
  40.  
  41. catch (Exception e) {
  42. System.err.println(e); // Print the exception to warn.
  43. }//end catch
  44. }//end main
  45. }//end class
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 10
Reputation: Meldroz is an unknown quantity at this point 
Solved Threads: 0
Meldroz Meldroz is offline Offline
Newbie Poster

Re: Need help with Java Assignment

 
0
  #2
Jul 11th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Need help with Java Assignment

 
0
  #3
Jul 11th, 2005
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:
  1. ArrayList first = new ArrayList();
  2. ArrayList second = new ArrayList();
  3. ArrayList third = new ArrayList();
  4. ArrayList fourth = new ArrayList();
  5. int count = 1;
  6. while (br.readLine() != null)
  7. {
  8.  
  9. StringTokenizer st = new StringTokenizer(br.readLine());
  10. while (st.hasMoreTokens())
  11. {
  12. if (count == 1)
  13. {
  14. first.add(st.nextToken());
  15. }
  16. else if (count == 2)
  17. {
  18. second.add(st.nextToken();
  19. }
  20. }
  21. }
You get the drift.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 2
Reputation: JOskydive is an unknown quantity at this point 
Solved Threads: 0
JOskydive JOskydive is offline Offline
Newbie Poster

Re: Need help with Java Assignment

 
0
  #4
Jul 14th, 2005
Thanks for the help. I finally got the program working. I posted the final code below.

Thanks again,
Jim

  1. public static void main(String args[])throws IOException {
  2.  
  3. BufferedReader fileIn = new BufferedReader(new InputStreamReader (System.in));
  4.  
  5. int count = 0;
  6. int MAX_LENGTH = 4;
  7. int partID = 0;
  8. String partName = " ";
  9. int partStock = 0;
  10. double partPrice = 0;
  11. CarPart [] array = new CarPart[MAX_LENGTH];//array
  12.  
  13. String fileName;
  14. System.out.print("Enter file name: ");
  15. fileName = fileIn.readLine();
  16.  
  17. try {
  18. BufferedReader in = new BufferedReader(new FileReader(fileName));
  19. String line;
  20.  
  21. while((line = in.readLine()) != null) { // Read line, check for end-of-file
  22. System.out.println("From File: " +line); // Print the line
  23.  
  24. StringTokenizer st = new StringTokenizer(line);//tokenizer
  25. partID = Integer.parseInt(st.nextToken());
  26. partName = st.nextToken();
  27. partStock = Integer.parseInt(st.nextToken());
  28. partPrice = Double.parseDouble(st.nextToken());
  29.  
  30. CarPart cp = new CarPart(partID, partName, partStock, partPrice);
  31.  
  32. if (count < MAX_LENGTH){
  33. array[count]=cp;
  34. count++;
  35.  
  36. }//end if
  37. //line = in.readLine();
  38. }//end while
  39. in.close(); // Always close a stream when you are done with it
  40.  
  41. }//end try
  42. catch (IOException e) {
  43. // Handle FileNotFoundException, etc. here
  44. }//end catch
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC