943,713 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1651
  • Java RSS
Sep 3rd, 2008
0

read from file

Expand Post »
i have a file from which i have to read and compute the total value of all items,i dont know what methodto use to read arrays
i tried DataInputStream(object).readInt(quantity[]); but it says it is wrong
can any one help me in reading from the file and computing the total value of all the items
the code is as follows

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.io.*;
  3. public class FiveProducts
  4. {
  5. static DataInputStream dis = new DataInputStream(System.in);
  6. static StringTokenizer st;
  7. public static void main(String args[])throws IOException
  8. {
  9. //create file where data to be stored
  10. FileOutputStream fos = new FileOutputStream("fivepro.dat");
  11. DataOutputStream dos = new DataOutputStream(fos);
  12.  
  13. //declare variables
  14. int prodcode[]= new int[4];
  15. double cost[]= new double[4];
  16. int quantity[]=new int[4];
  17. DataInputStream din = new DataInputStream(new FileInputStream("fivepro.dat"));
  18. //reading from console
  19. for(int i=0;i<4;i++)
  20. {
  21. System.out.println("enter code number for items = " + i);
  22. st = new StringTokenizer(dis.readLine());
  23. prodcode[i] = Integer.parseInt(st.nextToken());
  24. dos.writeInt(prodcode[i]); //write to file
  25.  
  26. }
  27. for(int j=0;j<4;j++)
  28. {
  29. System.out.println("enter cost of item = " + j);
  30. st = new StringTokenizer(dis.readLine());
  31. cost[j]= new Double(st.nextToken()).doubleValue();
  32. dos.writeDouble(cost[j]); // write to file
  33.  
  34. }
  35.  
  36. for(int k=0;k<4;k++)
  37. {
  38. System.out.println("enter quantity of item = " + k);
  39. st = new StringTokenizer(dis.readLine());
  40. quantity[k] = Integer.parseInt(st.nextToken());
  41. dos.writeInt(quantity[k]); // write to file
  42.  
  43. }
  44. dos.close();
  45. din.close();
  46.  
  47.  
  48. }
  49. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
newtechie is offline Offline
33 posts
since Jul 2008
Sep 4th, 2008
0

Re: read from file

just read the file, line by line, and add the value of that line to your array

just a bit the logic to follow, not 100% correct or tested code
Java Syntax (Toggle Plain Text)
  1. line = readNewLine();
  2. ArrayList values = new ArrayList();
  3. while (line != null){
  4. values.add(line);
  5. line = readNewLine();
  6. }
in the method readNewLine() you can then specify what kind of var line is going to be.
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is offline Offline
2,487 posts
since Jan 2007
Sep 4th, 2008
0

Re: read from file

Use the BufferedReader class with stultuske's code. This class contains the readLine() method you need
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Sep 4th, 2008
0

Re: read from file

Click to Expand / Collapse  Quote originally posted by javaAddict ...
Use the BufferedReader class with stultuske's code. This class contains the readLine() method you need
should we not use buffered reader for reading character,i have used datainput and dataoutput stream to read and write primitive data type.
if i am wrong correct me
Reputation Points: 10
Solved Threads: 0
Light Poster
newtechie is offline Offline
33 posts
since Jul 2008
Sep 5th, 2008
0

Re: read from file

i refined the code and now it reaches the end of file before printing the final value can anyone pls. help me out .
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.io.*;
  3. public class Fiveproducts1
  4. {
  5.  
  6. static DataInputStream dis = new DataInputStream(System.in);
  7. static StringTokenizer st;
  8. public static void main(String args[])throws IOException
  9. {
  10. //create file where data to be stored
  11. FileOutputStream fos = new FileOutputStream("fivepro.dat");
  12. BufferedOutputStream bos = new BufferedOutputStream(fos);
  13. DataOutputStream dos = new DataOutputStream(bos);
  14.  
  15. //declare variables
  16. int prodcode[]= new int[4];
  17. int cost[]= new int[4];
  18. int quantity[]=new int[4];
  19. FileInputStream fis = new FileInputStream("fivepro.dat");
  20. BufferedInputStream bis = new BufferedInputStream(fis);
  21. DataInputStream din = new DataInputStream(bis);
  22. //reading from console
  23. for(int i=0;i<prodcode.length;i++)
  24. {
  25. System.out.println("enter code number for items = " + i);
  26. st = new StringTokenizer(dis.readLine());
  27. prodcode[i] = Integer.parseInt(st.nextToken());
  28. dos.writeInt(prodcode[i]); //write to file
  29.  
  30.  
  31. System.out.println("enter cost of item = " + i);
  32. st = new StringTokenizer(dis.readLine());
  33. cost[i]= Integer.parseInt(st.nextToken());
  34. dos.writeInt(cost[i]); // write to file
  35.  
  36.  
  37. System.out.println("enter quantity of item = " + i);
  38. st = new StringTokenizer(dis.readLine());
  39. quantity[i] = Integer.parseInt(st.nextToken());
  40. dos.writeInt(quantity[i]); // write to file
  41.  
  42. }
  43. try
  44. {
  45. int Itemcost = din.readInt();
  46. int Quantity = din.readInt();
  47. int totalcost = Itemcost * Quantity ;
  48. System.out.println("total cost of items = " + totalcost);
  49. }
  50. catch(EOFException e)
  51. {
  52. System.err.print(e);
  53. }
  54. dos.close();
  55. din.close();
  56.  
  57. }
  58. }
Reputation Points: 10
Solved Threads: 0
Light Poster
newtechie is offline Offline
33 posts
since Jul 2008

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: how to acquire IPaddress using hostname??
Next Thread in Java Forum Timeline: Facing Pblm with Java Swings * MySQL





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


Follow us on Twitter


© 2011 DaniWeb® LLC