I am having trouble using FileReader to write a txt file to an array (Java), what am I doing wrong?

Posted by deliriumtremens on Stack Overflow See other posts from Stack Overflow or by deliriumtremens
Published on 2010-04-10T13:12:03Z Indexed on 2010/04/10 13:13 UTC
Read the original article Hit count: 461

Scanner s = null;
    try {
        s = new Scanner(new BufferedReader(new FileReader("rates.txt")));
            for (int i=0; i<9; i++){
                while(s.hasNext()){rates[i] = s.next();}
                System.out.println(rates[i]);
            }
    }catch (IOException e){
        System.out.println(e);
    }
    finally {
        if (s != null) {
            s.close();
        }
    }

When I run this code, it reads the last chunk of characters in my txt file, places them in rates[0], sticks null in 1-8, then puts that same last chunk in rates[9]. I'm not sure why it's reading the end of my file first. The contents of the txt are below..

USD 1.34

EUR 1.00

JPY 126.28

GBP 0.88

INR 60.20

It reads the 60.20, which is all it is recording in the array. Any help would be appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about filereader