Kestrel
Kestrel

Reputation: 587

How to read doubles from .txt file into an ArrayList

I'm trying to read doubles from a file that contains 100,000 doubles, arranged in lines of two doubles each separated by a space. Like this:

2.54343 5.67478
1.23414 5.43245
7.64748 4.25536
...

My code so far:

Scanner numFile = new Scanner(new File("input100K.txt").getAbsolutePath());
ArrayList<Double> list = new ArrayList<Double>();
while (numFile.hasNextLine()) {
    String line = numFile.nextLine();
    Scanner sc = new Scanner(line);
    sc.useDelimiter(" ");
    while(sc.hasNextDouble()) {
        list.add(sc.nextDouble());
    }
    sc.close();
}
numFile.close();
System.out.println(list);
}

After this code runs, it prints to the console and empty ArrayList [], and I can't figure out why.

Removing the getAbsolutePath() from the file gives me this line:

Scanner numFile = new Scanner(new File("input100K.txt"));

but when I run the program, it is giving me a FileNotFoundException. I know the file exists, I can see it and open it.

input100K.txt is located in the src package folder along with the program. is there somewhere specific where the file must be for this to work?

EDIT: As Evgeniy Dorofeev pointed out, the file needs to be in the project folder (parent of src folder) for the program to find it.

Upvotes: 3

Views: 2285

Answers (4)

Visruth
Visruth

Reputation: 3450

Here is a sample program :

public static void main(String[] args) throws FileNotFoundException {

        List<Double> doubleList = new ArrayList<Double>();
        Scanner scanner = new Scanner(new File("/home/visruthcv/inputfile.txt"));
        while (scanner.hasNextDouble()) {

            double value = scanner.nextDouble();

            System.out.println(value);

            doubleList.add(value);

        }
        /*
         * for test print
         */
        for (Double eachValue : doubleList) {
            System.out.println(eachValue);
        }

    }

Upvotes: 0

easynoder
easynoder

Reputation: 104

replace the line

Scanner numFile = new Scanner(new File("input100K.txt").getAbsolutePath());

with

Scanner numFile = new Scanner(new File("input100K.txt"));

Upvotes: 0

Christian Tapia
Christian Tapia

Reputation: 34146

You can try using split() method on the line you just have read, then parsing each part to a double. Note that it's not necessary to create two Scanners:

Scanner sc = new Scanner(new File("input100K.txt"));
sc.useDelimiter(" ");
ArrayList<Double> list = new ArrayList<Double>();

while (sc.hasNextLine()) {
    String[] parts = sc.nextLine().split(" "); // split each line by " "
    for (String s : parts) {
        list.add(Double.parseDouble(s)); // parse to double
    }
}
sc.close();

System.out.println(list);

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

When you create Scanner like this new Scanner(new File("input100K.txt").getAbsolutePath()); you are scanning file path as input not file itself. Do it this way new Scanner(new File("input100K.txt"));

Upvotes: 4

Related Questions