944,175 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3416
  • Java RSS
Jul 11th, 2005
0

Need help with Java Assignment

Expand Post »
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
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JOskydive is offline Offline
2 posts
since Jul 2005
Jul 11th, 2005
0

Re: Need help with Java Assignment

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Meldroz is offline Offline
11 posts
since Mar 2004
Jul 11th, 2005
0

Re: Need help with Java Assignment

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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 14th, 2005
0

Re: Need help with Java Assignment

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

Thanks again,
Jim

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JOskydive is offline Offline
2 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Connecting to Oracle through Blackberry Handheld Device
Next Thread in Java Forum Timeline: get current time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC