Achilleas Eftychiou Achilleas Eftychiou - 4 years ago 92
Java Question

How to determine the size of array from txt file

This is a part of my code and I want to know how to determine the size of array from a txt file:

FileInputStream w = new FileInputStream(args[0]); // reads a file
Scanner scanner = new Scanner(w);
while (scanner.hasNextDouble())
Double[] con = new Double[15]; //makes array
for (int i = 0; i < con.length; i++)
con[i] = scanner.nextDouble(); // data is saved in array

Answer Source

you can define a new variable named int sum =0; and underneath while(scanner.hasNextDouble()){ you can add sum++; to count the length of the array and make your Double [] con= new Double [sum];

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download