3

I am working on an assignment with the following instructions,

Write a method that will load an array of floating point numbers from a file named floats.txt that you create, and then return that array. Assume the first value in the file holds the size of the array. Be sure to call your method.

I have created the following file with the title floats.txt

5
  4.3
  2.4
  4.2
  1.5
  7.3

I have never written a method that will return an array, nor have I ever created an array that reads from a file. Not asking anyone to write the program for me, by any means, but would appreciate some suggestions to get me started. I have written the method header as follows,

  public static double[] floatingFile() throws IOException {
  Scanner fin = new Scanner(new File"floats.txt");

Thanks.

6 Answers6

1

For Java 7 I would use 1 row code:

List<String> lines = Files.readAllLines(Paths.get("floats.txt"), StandardCharsets.UTF_8);

Read all lines from a file. This method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown. Bytes from the file are decoded into characters using the specified charset.

See Doc here

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
0

You need to read the file into memory, you can do that line by line following what is here:

How to read a large text file line by line using Java?

Once you have a line of the file you can add it to your array.

Community
  • 1
  • 1
ddoor
  • 5,819
  • 9
  • 34
  • 41
0

Have a look at the javadoc for Scanner: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

There are methods such as Scanner.next() that will return the next value in the file.

0

You can open the file and read the elements one by one while storing them in each array element till it reaches null. Are you familiar with ArrayList?

Basilisk
  • 13
  • 1
  • 2
  • 6
  • I am not familiar with ArrayList, I'm still pretty new at this. –  Nov 24 '13 at 10:26
  • Well, you can use ArrayList which is an array implementation using the List interface. using this, you will not need to find the size of the file first in order to initialize your array. http://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx might be helpful. – Basilisk Nov 24 '13 at 10:40
  • To get you started you need to do the following: For a method returning an array, your method should be in the form `public arrayList[] readLines (String )`. Now that you have your file opened, you can use `FileReader` and `BufferedReader` objects (check the link pasted by coder to learn filehandling) ArrayList contains methods like `.add(), remove()` to add/remove elements from the arrayList Using the while loop, you can read your file until it reaches null and add elements to the arrayList using arrayList.add() To convert the arrayList to array, 'use arrayList.toArray()' – Basilisk Nov 24 '13 at 10:54
  • Check the piece of code I've posted in a new answer, using the implementation I've explained above – Basilisk Nov 24 '13 at 11:08
0

I would pass the path of the file to your constructor (as follows), you still need to add one or two things... see here for example.

/**
 * Read in a file containing floating point numbers and
 * return them as an array.
 * 
 * @param filePath
 *          The path to the file to read.
 * @return Any double(s) read as an array.
 */
public static double[] floatingFile(String filePath) {
  Scanner fin = null;
  try {
    // Open the file at filePath.
    fin = new Scanner(new File(filePath));
    // first read the size
    int advSize = 0;
    if (fin.hasNextInt()) {
      // read in an int.
      advSize = // DO SOMETHING TO READ AN INT.
    }
    // construct a dynamic list to hold the Double(s).
    List<Double> al = new ArrayList<Double>(advSize);
    // while there are Double(s) to read.
    while (fin.hasNextDouble()) {
      // read in a double.
      double d = // DO SOMETHING TO READ A DOUBLE.
      al.add(d);
    }
    // Construct the return array.
    double[] ret = new double[al.size()];
    for (int i = 0; i < ret.length; i++) {
      ret[i] = al.get(i);
    }
    return ret;
  } catch (FileNotFoundException ignored) {
  } finally {
    if (fin != null) {
      // close our file reader.
      fin.close();
    }
  }
  // Return the empty array.
  return new double[] {};
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0
 public float[] readLines(String <your filename>){ //in the try block
    FileReader fileReader = new FileReader(filename);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    List<float> arrFloat = new ArrayList<float>();
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        lines.add(line);
    }
    bufferedReader.close();
    return arrFloat.toArray(new floatArray[arrFloat]);
}
Basilisk
  • 13
  • 1
  • 2
  • 6