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

import java.util.*;
import java.io.*;
public class FiveProducts
{
    static DataInputStream dis = new DataInputStream(System.in);
        static StringTokenizer st;
    public static void main(String args[])throws IOException
    {              
        //create file where data to be stored
        FileOutputStream fos = new FileOutputStream("fivepro.dat");
        DataOutputStream dos = new DataOutputStream(fos);
        
           //declare variables
      int prodcode[]= new int[4];
      double cost[]= new double[4];
      int quantity[]=new int[4];
        DataInputStream din = new DataInputStream(new FileInputStream("fivepro.dat"));
        //reading from console
      for(int i=0;i<4;i++)
      {
        System.out.println("enter code number for items = " + i);
        st = new StringTokenizer(dis.readLine());
         prodcode[i] = Integer.parseInt(st.nextToken());
        dos.writeInt(prodcode[i]);  //write to file
        
    }
      for(int j=0;j<4;j++)
      {
          System.out.println("enter cost of item = " + j);
          st = new StringTokenizer(dis.readLine());
          cost[j]= new Double(st.nextToken()).doubleValue();
          dos.writeDouble(cost[j]); // write to file
          
      }    
      
      for(int k=0;k<4;k++)
      {
          System.out.println("enter quantity of item = " + k);
          st = new StringTokenizer(dis.readLine());
          quantity[k] = Integer.parseInt(st.nextToken());
          dos.writeInt(quantity[k]); // write to file
        
       }
      dos.close();
      din.close();
     
   
   }          
}

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 :)

line = readNewLine();
ArrayList values = new ArrayList();
while (line != null){
   values.add(line);
   line = readNewLine();
}

in the method readNewLine() you can then specify what kind of var line is going to be.

Use the BufferedReader class with stultuske's code. This class contains the readLine() method you need

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

i refined the code and now it reaches the end of file before printing the final value can anyone pls. help me out .

import java.util.*;
import java.io.*;
public class Fiveproducts1
{

    static DataInputStream dis = new DataInputStream(System.in);
        static StringTokenizer st;
    public static void main(String args[])throws IOException
    {              
        //create file where data to be stored
        FileOutputStream fos = new FileOutputStream("fivepro.dat");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        DataOutputStream dos = new DataOutputStream(bos);
        
           //declare variables
      int prodcode[]= new int[4];
      int cost[]= new int[4];
      int quantity[]=new int[4];
       FileInputStream fis = new FileInputStream("fivepro.dat");
       BufferedInputStream bis = new BufferedInputStream(fis);
        DataInputStream din = new DataInputStream(bis);
        //reading from console
      for(int i=0;i<prodcode.length;i++)
      {
        System.out.println("enter code number for items = " + i);
        st = new StringTokenizer(dis.readLine());
         prodcode[i] = Integer.parseInt(st.nextToken());
        dos.writeInt(prodcode[i]);  //write to file
   
       
         System.out.println("enter cost of item = " + i);
          st = new StringTokenizer(dis.readLine());
          cost[i]= Integer.parseInt(st.nextToken());
          dos.writeInt(cost[i]); // write to file
        
                 
          System.out.println("enter quantity of item = " + i);
          st = new StringTokenizer(dis.readLine());
          quantity[i] = Integer.parseInt(st.nextToken());
          dos.writeInt(quantity[i]); // write to file
        
       }
         try
         {     
        int Itemcost = din.readInt();
        int Quantity = din.readInt();
        int totalcost = Itemcost * Quantity ;
        System.out.println("total cost of items = " + totalcost);
         }
         catch(EOFException e)
         {
         System.err.print(e);
         }
         dos.close();
         din.close();
     
      }          
}
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.