Creating array from file; getting error: Exception in thread "main" java.util.NoSuchElementException: No line found -
all trying create array numbers in file... i'm pretty new java may not efficient way i'm going off limited knowledge now.
when run it, following message:
exception in thread "main" java.util.nosuchelementexception: no line found @ java.util.scanner.nextline(scanner.java:1540) @ createarray.main(createarray.java:27)
here feeble attempt @ 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 number of lines in file int count = 0; while (inputfile.hasnextline()) { string str = inputfile.nextline(); count++; } // create array double[] numbers = new double[count]; // add numbers array string str; while (inputfile.hasnextline()); { (int = 0; < count; i++) { str = inputfile.nextline(); numbers[i] = double.parsedouble(str); } } // display array (int = 0; < numbers.length; i++) system.out.print(numbers[i] + " "); } }
when write inputfile.hasnextline()
in code using scanner, have read line file.
@tima said, completed reading file in first while loop. try looking @ java: read txt file array
Comments
Post a Comment