Read Specific Data with Buffered Filereader

925 views Asked by At

From a text file I am trying to get the the 3rd set of data (type = double) in a row and then sum it up to get the total. My problem is that I am having trouble figuring out how to grab a specific piece of data out of a line with a buffered file reader. I know how to get the line, but parsing the data is the mystery. I have placed my code below in case it may help give more context. Thanks!

EDIT: Please bear with me. I'm literally within my first month of learning Java. I have to use buffered reader. This is a school project. Am I supposed to use "split"? If so can I store the "next split" or something into and array?

listings.txt

Int           string(?) double   int

PropertyID    Type    Cost     AgentID --(Not in the file. The file only has the data)

100000       Farm    500000.00   101

100001       Land    700000.00   104

Code

    package overview;

    import java.io.*;
    import java.util.*;
    import java.lang.*;
    import java.nio.*;

    public class Overview {

        public static void main(String[] args) throws FileNotFoundException {

            // TODO code application logic here
            int count = 0;  
            double totalCost=0.00;
            ArrayList<Double> propertyID = new ArrayList();


            //Get file name
            Scanner console = new Scanner(System.in);
            System.out.print ("Please enter file name: ");
            String inputFileName = console.next();
            File inputFile = new File(inputFileName);

            // Get the object of DataInputStream
            FileInputStream fstream = new FileInputStream(inputFile);
            DataInputStream in = new DataInputStream(fstream);


            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;

            try {
                while ((line = reader.readLine()) != null) 
                {
                    double x = Double.parseDouble(line.split(" ")[]);
                    propertyID.add(x);;
                    totalCost = Double.parseDouble(line.split(" ")[8]);
                    count++;
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        finally {

            System.out.println("Total properties in list: " + count + "\n"+ "The total cost is: " +totalCost);}
        }
    }
1

There are 1 answers

15
Moritz Petersen On BEST ANSWER

Here is an example, that works (without a file):

private static final String data = "100000    Farm    500000.00    101\n100001    Land    700000.00    104";

public static void main(String[] args) throws FileNotFoundException {
    int count = 0;
    double totalCost = 0;

    BufferedReader reader = new BufferedReader(new StringReader(data));
    String line;

    try {
        while ((line = reader.readLine()) != null) {
            StringTokenizer stok = new StringTokenizer(line);
            int propertyId = Integer.parseInt(stok.nextToken());
            String type = stok.nextToken();
            double cost = Double.parseDouble(stok.nextToken());
            int agentId = Integer.parseInt(stok.nextToken());

            totalCost += cost;
            count++;
        }
        // close input stream
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        System.out.println("Total properties in list: " + count + "\nTotal cost is: " + totalCost);
    }
}