Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:11:27+00:00 2026-06-04T05:11:27+00:00

From a text file I am trying to get the the 3rd set of

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:11:29+00:00Added an answer on June 4, 2026 at 5:11 am

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to tokenize my data that I get from my text file.
I'm trying to get all the text from a txt file into a string,
I’m trying to import fixed width data from text file into SQL Server Express
I am trying to input data from a text file in C++. The text
I'm trying to load a list of filenames from a text file and then
I'm trying to get data from file. The file first has three lines of
I'm trying to just get rid of duplicate consecutive words from a text file,
I am trying to use a textreader to retrieve data from a text file
I'm trying to load data from a text file on my server using the
I am trying to read in data from a text file using numpy.loadtxt with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.