Java: Reading a File into an Array

Java read file and store text in an array

Stored as strings:

public class ReadTemps {

public static void main(String[] args) throws IOException {
// TODO code application logic here

// // read KeyWestTemp.txt

// create token1
String token1 = "";

// for-each loop for calculating heat index of May - October

// create Scanner inFile1
Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt")).useDelimiter(",\\s*");

// Original answer used LinkedList, but probably preferable to use ArrayList in most cases
// List<String> temps = new LinkedList<String>();
List<String> temps = new ArrayList<String>();

// while loop
while (inFile1.hasNext()) {
// find next line
token1 = inFile1.next();
temps.add(token1);
}
inFile1.close();

String[] tempsArray = temps.toArray(new String[0]);

for (String s : tempsArray) {
System.out.println(s);
}
}
}

For floats:

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class ReadTemps {

public static void main(String[] args) throws IOException {
// TODO code application logic here

// // read KeyWestTemp.txt

// create token1

// for-each loop for calculating heat index of May - October

// create Scanner inFile1
Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt")).useDelimiter(",\\s*");

// Original answer used LinkedList, but probably preferable to use ArrayList in most cases
// List<Float> temps = new LinkedList<Float>();
List<Float> temps = new ArrayList<Float>();

// while loop
while (inFile1.hasNext()) {
// find next line
float token1 = inFile1.nextFloat();
temps.add(token1);
}
inFile1.close();

Float[] tempsArray = temps.toArray(new Float[0]);

for (Float s : tempsArray) {
System.out.println(s);
}
}
}

Read text file into an array

Use an ArrayList or an other dynamic datastructure:

BufferedReader abc = new BufferedReader(new FileReader(myfile));
List<String> lines = new ArrayList<String>();

while((String line = abc.readLine()) != null) {
lines.add(line);
System.out.println(data);
}
abc.close();

// If you want to convert to a String[]
String[] data = lines.toArray(new String[]{});

Reading a text file into an array and performing a sort in Java

You are creating an array of length 100000 and fill the lines as they are read. Initially all elements will be null and after reading the file quite a number of them is likely to still be null. Thus when you sort the array numbers[j] will eventually be a null element and thus calling compareTo(...) on that will throw a NullPointerException.

To fix that you need to know where in the array the non-null part ends. You are already tracking the number of read lines in index so after reading the file that would be the index of the first null element.

Now you basically have 2 options:

  • Pass index to bubbleV1String() and do for(int i = 0; i < index-1; i++) etc.
  • Make a copy of the array after reading the lines and before sorting it:
    String[] copy = new String[index];
StringSystem.arrayCopy(myArray,0,copy,0,index);
//optional but it can make the rest of the code easier to handle: replace myArray with copy
myArray = copy;

Finally you could also use a List<String> which would be better than using arrays but I assume that's covered by a future lesson.



Related Topics



Leave a reply



Submit