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.
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.