| | |
read from file
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 28
Reputation:
Solved Threads: 0
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
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)
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
in the method readNewLine() you can then specify what kind of var line is going to be.
just a bit the logic to follow, not 100% correct or tested code

Java Syntax (Toggle Plain Text)
line = readNewLine(); ArrayList values = new ArrayList(); while (line != null){ values.add(line); line = readNewLine(); }
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
•
•
Join Date: Jul 2008
Posts: 28
Reputation:
Solved Threads: 0
•
•
•
•
Use the BufferedReader class with stultuske's code. This class contains the readLine() method you need
if i am wrong correct me
•
•
Join Date: Jul 2008
Posts: 28
Reputation:
Solved Threads: 0
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)
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(); } }
![]() |
Similar Threads
- Move file pointer to read next file value (C++)
- read from file help (C++)
- write and read to file (C++)
- how to read a file from client side without browsing (ASP.NET)
- C++ How can read from file.txt & where can save this file(file.txt) to start reading (C++)
- create, write & read file to/from folder (C++)
- Read in a file and store in char array (C)
Other Threads in the Java Forum
- Previous Thread: how to acquire IPaddress using hostname??
- Next Thread: Facing Pblm with Java Swings * MySQL
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp draw eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






