1

I have a method that reads in a file. The first line of the file has an integer that shows how many additional lines there are. Each additional line after that has a pair of two double values (no commas)

For Example:

4

13 15

20.2 33.5

24 38

30 31

etc.

I have two static double arrays, x and y. For each line after the first, I want to assign the first double in the set to x, and the second double in the set to y. However, i'm not 100% sure what to do how to assign. This is my code so far: (note that line is a call to another method not shown)

Scanner input = new Scanner(System.in);
    System.out.println("Enter the name of the data file.");
    fileName = input.nextLine();

    while(input.hasNext())
    {
        int num = input.nextInt(); // the first line integer
        line.x[0] = input.nextDouble(); //the additional line x coordinate
        line.y[0] = input.nextDouble(); //the additional line y coordinate
    }

The only problem is, how do I increment the value of x and y from [0] to [1], [2], 3, etc for each additional line in the file, based on what the first line int value 'num' is, so that I don't keep overwriting [0]?

For example, in the example above, the value of 'num' is 4, because there are four additional lines after it. How do I increase (+=) the value of x and y by one based on the value of num? I know this sounds stupid but I'm stumped at this point.

2
  • Something doesn't look right in your code. You show filename = input.nextLine(), then the while loop processes the input scanner. Did you instead plan to use a file reader? Or are you really reading from the console input? I'm just letting you know something looks amiss. Commented Oct 22, 2014 at 4:33
  • @AlvinBunk just reading from the console input :) Commented Oct 22, 2014 at 16:03

2 Answers 2

3

The only problem is, how do I increment the value of x and y from [0] to [1], [2], 3, etc for each additional line in the file, based on what the first line int value 'num' is, so that I don't keep overwriting [0]?

Use a counter i

int num = input.nextInt(); // the first line integer
int i = 0;

while(input.hasNext())
{
    line.x[i] = input.nextDouble(); //the additional line x coordinate
    line.y[i++] = input.nextDouble(); //the additional line y coordinate
}
Sign up to request clarification or add additional context in comments.

4 Comments

Just as a style note: for reasons of clarity I usually prefer to avoid using the postincrement in any complex expression. While there's no danger of this one going wrong, it's still a good rule. Hence the only difference between our two versions. :)
Could I also use a for loop?
Yep I would prefer a for loop, since u know the #input in advance n.
@MiteshPathak So this will add additional values for each line then, (like x[1], x[2], x[3],x[4] etc
1
  line.x[0] = input.nextDouble();

This assigns the next value read to the first slot in the array x belonging to an object called line (I'm assuming that line is an object you have instantiated and that x is a public field of that object...)

You want to increment an index and use that to address your arrays:

int i = 0;
while(input.hasNext())
{
    int num = input.nextInt(); // the first line integer
    line.x[i] = input.nextDouble(); //the additional line x coordinate
    line.y[i] = input.nextDouble(); //the additional line y coordinate
    i ++;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.