0

I have a text file with a list of doubles. The first value of the line is the x value and the second value is the y value.

  103.0 274.0
  133.0 383.0
  342.0 250.0
  204.0 126.0
  177.0 357.0
  ...

How can I read these values and load them into an array list?

  ArrayList<Point> store = new ArrayList<Point>();


                  File file = fc.getSelectedFile();

               StringBuilder all = new StringBuilder();

               BufferedReader reader = new BufferedReader( new FileReader(file));
               String input = null;
               while ((input = reader.readLine()) != null)
               {
                 String a =  all.append(input+"\n").toString();

                 String[] hold = a.split(" ");

                double x =  Double.parseDouble(hold[0]);
                int aa = (int)(x);
                double y = Double.parseDouble(hold[1]);
                int bb = (int)(y);

                store.add(new Point(aa, bb));
                }
chief
  • 730
  • 1
  • 9
  • 19
  • 1
    http://stackoverflow.com/questions/1855753/reading-double-values-from-a-file – MD Sayem Ahmed Mar 19 '12 at 05:09
  • please take a bit more care when formatting your code (this forum can't handle tabs smoothly, especially not when mixed with spaces - as you can see :-) – kleopatra Mar 19 '12 at 11:22

3 Answers3

1
1: Get an input stream of the file.
2: read line by line using readLine() method
3: split the string by SPACE using split() method of String class
4: the String array you got has two elements now.
5: Double.parseDouble(array[0]) and Double.parseDouble(array[1]) are value of x and y
6: store these values into the corresponding values of Point object
7: add that point object to ArrayList.
8: Done
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • Well I worked on it a bit. I can't get it to work correctly. See above. – chief Mar 19 '12 at 05:58
  • Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "178.0 306.0" The first line of the file is: 349.0 178.0 and the second line of the file is: 306.0 159.0 – chief Mar 19 '12 at 06:14
  • @chief instead of taking a StringBuilder appending it and reconverting toString, try using the value input only. I mean in the while loop, remove the first line and in the 2nd line instead of a.split use input.split. – Chandra Sekhar Mar 19 '12 at 06:21
  • @chief while ((input = reader.readLine()) != null) { String[] hold = input.split(" "); double x = Double.parseDouble(hold[0]); int aa = (int)(x); double y = Double.parseDouble(hold[1]); int bb = (int)(y); store.add(new Point(aa, bb)); } – Chandra Sekhar Mar 19 '12 at 06:24
  • Thanks. It is now taking the file and loading the data into the arrayList. – chief Mar 19 '12 at 06:27
  • @chief previously your problem was because of the append of \n character – Chandra Sekhar Mar 19 '12 at 06:28
1

using java Scanner and nextDouble()

Scanner is use to read file and parameter nextDouble() is data type in read file

sique
  • 241
  • 1
  • 2
  • 12
0

You can use resource bundle in java.

kundan bora
  • 3,821
  • 2
  • 20
  • 29