read from file

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

Join Date: Jul 2008
Posts: 28
Reputation: newtechie is an unknown quantity at this point 
Solved Threads: 0
newtechie newtechie is offline Offline
Light Poster

read from file

 
0
  #1
Sep 3rd, 2008
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: read from file

 
0
  #2
Sep 4th, 2008
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,683
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: read from file

 
0
  #3
Sep 4th, 2008
Use the BufferedReader class with stultuske's code. This class contains the readLine() method you need
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 28
Reputation: newtechie is an unknown quantity at this point 
Solved Threads: 0
newtechie newtechie is offline Offline
Light Poster

Re: read from file

 
0
  #4
Sep 4th, 2008
Originally Posted by javaAddict View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 28
Reputation: newtechie is an unknown quantity at this point 
Solved Threads: 0
newtechie newtechie is offline Offline
Light Poster

Re: read from file

 
0
  #5
Sep 5th, 2008
i refined the code and now it reaches the end of file before printing the final value can anyone pls. help me out .
  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. }
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC