S_Wheelan
S_Wheelan

Reputation: 267

Reading doubles from .txt file into a List

I'm trying to read doubles from a .txt file and put them all into a list.

Here's my code so far ->

(I have one method to ask for the file and get the data stream, and one to put the doubles into a list.)

    public InputFile () throws MyException {
    fileIn = null;
    dataIn = null;
    do {
        filename = JOptionPane.showInputDialog("What is the file called? ");
        try {
            fileIn = new FileInputStream((filename + ".txt"));
            dataIn = new DataInputStream(fileIn);

            return;
        }
            catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "There is no "+filename + ".txt");
        }
   }
   while (   Question.answerIsYesTo("Do you want to retype the file name")   );
   throw new MyException("No input file was chosen");
}

That part works fine.

        public ProcessMain() {
        boolean EOF = false;

    List <Double> allNumbers = new ArrayList <Double> () ;

            try {

                InputFile inputFile = new InputFile();

                        while(EOF == false) {
                        try {
                          allNumbers.add(inputFile.dataIn.readDouble());
                            }
                        catch(EOFException e){ EOF = true; }
                        }

         // List manipulation here. I have no problems with this part.

    }
    catch (Exception e) {
        System.out.println(e);
    }

     System.out.println(allNumbers);

I get the following -

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Any ideas?

Upvotes: 0

Views: 3146

Answers (3)

bmargulies
bmargulies

Reputation: 100013

DataInputStream is only for reading things written with the corresponding output stream. For a text file, create an InputStreamReader around your FileInputStream (remembering to specify the encoding), and then a BufferedReader so that you can read by lines. Then use the Double class to parse the strings.

Upvotes: 0

ewan.chalmers
ewan.chalmers

Reputation: 16235

DataInputStream is really for reading binary data, e.g. from a socket. I think what you want is just simple FileReader and then parse using Double.parseDouble(String) or Double.valueOf(String) - depending on whether you want to get primitive or Object doubles.

If your input file contains each number on a new line, you can use BufferedReader.readLine. Otherwise you will need some simple parsing, e.g. to find space or comma-separated values.

So for example, to read a file containing each number on a new line you could do something like this:

import java.io.*;

public class DoubleReader {
  public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(args[0]));
    String line = null;
    while((line = reader.readLine()) != null) {
        Double d = Double.valueOf(line);
        System.err.println(d);
    }
  }
}

But if your file separates the numbers differently (e.g. space or comma) you will need to do a bit of extra/different work while reading the file to extract (parse) your values.

Parsing is about tokenizing your data so you can extract the bits you're interested in.

Upvotes: 2

roberttdev
roberttdev

Reputation: 46513

It's probably the "EOF = false" line.. that assigns "false" to "EOF" and will always be true. Try changing it to "EOF == false".

Upvotes: 0

Related Questions