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 8963637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:24:02+00:00 2026-06-15T16:24:02+00:00

I am trying to read in a text file line by line. On each

  • 0

I am trying to read in a text file line by line. On each line there are two double numbers separated by a comma (representing x,y points). I would like to put them in two arrays of doubles (one containing the x-points and the other the y-points) in order that they are in the file. The arrays should be the same size as the number of points there are in the file (i.e. the number of lines in the file).

The files could have different numbers of points, so I do not want to declare a fixed size for all files. How do I go about doing this?

EDIT:

There is something wrong with the way I am doing this:

BufferedReader input; 

ArrayList<Double> xpointArrayList = new ArrayList<Double>();
ArrayList<Double> ypointArrayList = new ArrayList<Double>();
try {
    input = new BufferedReader(new FileReader(args[0]));
    String line;
    while ((line = input.readLine()) != null) {
        line = input.readLine();
        String[] splitLine = line.split(",");

        double xValue = Double.parseDouble(splitLine[0]);
        double yValue = Double.parseDouble(splitLine[1]);

        xpointArrayList.add(xValue);
        ypointArrayList.add(yValue);
    }
    input.close();

    } catch (IOException e) {

    } catch (NullPointerException npe) {

    }

    double[] xpoints = new double[xpointArrayList.size()];
    for (int i = 0; i < xpoints.length; i++) {
        xpoints[i] = xpointArrayList.get(i);
    }
    double[] ypoints = new double[ypointArrayList.size()];
    for (int i = 0; i < ypoints.length; i++) {
        ypoints[i] = ypointArrayList.get(i);
    }

When I do the Array.toSring call on the xpoints and the ypoints array. It only has one number. For example in the file:

1,2 / 3,4 / 0,5

It only has 3.0 for the xpoints array and 4.0 for the ypoints array. Where is it going wrong?

  • 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-15T16:24:04+00:00Added an answer on June 15, 2026 at 4:24 pm

    How about just making a single ArrayList of type Point. An ArrayList will grow to the size of the content, so it should be as simple as doing something like this (psuedocode)…

    ArrayList<Point> points = new ArrayList<Point>();
    
    for (each line of the file){
        String line = file.readLine();
        String[] splitLine = line.split(",");
    
        double xValue = Double.parseDouble(splitLine[0]);
        double yValue = Double.parseDouble(splitLine[1]);
    
        points.add(new Point(xValue,yValue));
    }
    

    The Point class is designed as a container for 2 double values, used to represent any abstract point, which should suit your need perfectly. Refer to the Point documentation.

    If you really need to use 2 arrays, my recommendation is to use an ArrayList<Double>…

    ArrayList<Double> xPoints = new ArrayList<Double>();
    ArrayList<Double> yPoints = new ArrayList<Double>();
    
    for (each line of the file){
        String line = file.readLine();
        String[] splitLine = line.split(",");
    
        double xValue = Double.parseDouble(splitLine[0]);
        double yValue = Double.parseDouble(splitLine[1]);
    
        xPoints.add(xValue);
        yPoints.add(yValue);
    }
    

    Refer to the ArrayList documentation for more information about this.

    If you really need to use arrays, you can convert the ArrayList to an array like this…

    Double[] xArray = (Double[])xPoints.toArray();
    Double[] yArray = (Double[])yPoints.toArray();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read the last line from a text file. Each line starts
I'm trying to read a text file line by line, and add each line
I am trying to read a text file and adding each line to a
I'm trying to read in a text file line by line and process each
So, I'm trying to read from a text file and store each line into
I am trying to read a text file line by line, and the text
I'm trying to read a line of text from a text file and put
What I am trying to do is read from a text file where each
I am trying to read some text files, where each line needs to be
I'm trying to read a text file, and then break it up by each

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.