All I am trying to do is create an array from numbers in a file... I'm pretty new to java so it may not be the most efficient way but I'm going off limited knowledge for now.
When I run it, I get the following message:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at CreateArray.main(CreateArray.java:27)
Here is my feeble attempt at the code:
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class CreateArray
{
public static void main(String[] args) throws IOException
{
File file = new File("Numbers.txt");
Scanner inputFile = new Scanner(file);
// Find the number of lines in the file
int count = 0;
while (inputFile.hasNextLine())
{
String str = inputFile.nextLine();
count++;
}
// Create array
double[] numbers = new double[count];
// Add numbers to array
String str;
while (inputFile.hasNextLine());
{
for (int i = 0; i < count; i++)
{
str = inputFile.nextLine();
numbers[i] = Double.parseDouble(str);
}
}
// Display array
for (int i = 0; i < numbers.length; i++)
System.out.print(numbers[i] + " ");
}
}