tags:

views:

152

answers:

3

I'm trying to read some numbers (double) from a file and store them in an ArrayList and an array (yes, I need both) with the code below:

try {
 Scanner scan = new Scanner(file).useDelimiter("\\s*\\n");

 while(scan.hasNextDouble())
 {
  tmp.add(scan.nextDouble());
 }

 Double[][] tmp2 = new Double[tmp.size()/2][2];
 int tmp3 = 0;
 for(int i = 0; i < tmp.size()/2; i++)
 {
  for(int j = 0; j < 2; j++)
  {
   tmp2[i][j] = tmp.get(tmp3);
   tmp3++;
  }
 }

} catch (FileNotFoundException e1) {
 e1.printStackTrace();
}

}

The file I'm trying to read is:

0.0 0.0
0.023 0.023
0.05 0.05
0.2 0.2
0.5 0.5
0.8 0.8
0.950 0.950
0.977 0.977
1.0 1.0

But well my code doesn't work, the hasNextDouble() function doesn't find anything, what am I doing wrong?

EDIT: ok so I edited the source a bit (changed from Object[][] to Double[][]) and added inserting values into the array after they were inserted into the ArrayList, but it still doesn't work - the 'while' loop isn't executed a single time.

A: 

Try setting the delimiter first:

scan.useDelimiter("\\s+");

JavaDoc

BranTheMan
+1  A: 

I tried reducing the code down to only test the Scanner by itself. The following code works with your data file:

public static void main(String[] args) {
    Scanner scan;
    File file = new File("resources\\scannertester\\data.txt");
    try {
        scan = new Scanner(file);

        while(scan.hasNextDouble())
        {
            System.out.println( scan.nextDouble() );
        }

    } catch (FileNotFoundException e1) {
            e1.printStackTrace();
    }

}

I got the following (expected) output:

0.0
0.0
0.023
0.023
0.05
0.05
0.2
0.2
0.5
0.5
0.8
0.8
0.95
0.95
0.977
0.977
1.0
1.0

Try this to make sure you're referencing the correct file.

Bill the Lizard
Added the delimiter, but nothing changed. I added two System.out.println to see what's wrong, the one right before "while" is ok, but the one in the while loop doesn't print.
Zenzen
Hang on, testing...
Bill the Lizard
@Bill: I have trouble believing that. I just ran the code with no change and Scanner had no trouble reading the input.
Carl Smotricz
The code with no change doesn't even compile.
Bill the Lizard
sho nuff. I wrapped it into a main() and prepended definitions for tmp and file.
Carl Smotricz
Okay, that's the part I'm trying to work out. I just saw your comment to the question.
Bill the Lizard
I don't know why (will check it later), but it works, thanks.
Zenzen
All's well that ends well!
Carl Smotricz
+1  A: 

Below is my rendition of your code, adapted to make it run. It immediately explodes with an array indexing exceptions.

So: Can you give us a little more framework? What's different from what I did?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Zenzen {

   private static ArrayList<Double> tmp = new ArrayList<Double>();

   private static File file = new File("Zenzen.dat");

   public static void main(String[] args) {

      Scanner scan;
      try {
         scan = new Scanner(file);
         Object[][] tmp2 = new Object[tmp.size() / 2][2];
         int tmp3 = 0;

         while (scan.hasNextDouble()) {
            tmp.add(scan.nextDouble());
            System.out.println(Arrays.deepToString(tmp.toArray())); // debug print
            for (int i = 0; i < tmp.size() / 2; i++) {
               for (int j = 0; j < 2; j++) {
                  tmp2[i][j] = tmp.get(tmp3);
                  tmp3++;
               }
            }
         }

      } catch (FileNotFoundException fnfe) {
         fnfe.printStackTrace();
      }
   }
}


[0.0]
[0.0, 0.0]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Zenzen.main(Zenzen.java:26)
Carl Smotricz
Oh yeah I declared the tmp2 array too early, I changed it now.
Zenzen