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
tobubbleV1String()
and dofor(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
Java Type Inference: Reference Is Ambiguous in Java 8, But Not Java 7
How to Set an Image as a Background for Frame in Swing Gui of Java
Eclipse Error: "The Import Xxx Cannot Be Resolved"
Parsing a Date's Ordinal Indicator ( St, Nd, Rd, Th ) in a Date-Time String
Windowbuilder Design Tab Incomplete in Eclipse
Password Protected Zip File in Java
Error Upon Assigning Layout: Boxlayout Can't Be Shared
How to Launch a Completely Independent Process from a Java Program
How to Map a Postgresql Array with Hibernate
Maven Error: Could Not Find or Load Main Class Org.Codehaus.Plexus.Classworlds.Launcher.Launcher
How to Compare Two Java Objects
Killing Thread After Some Specified Time Limit in Java
Posting a @Onetomany Sub-Resource Association in Spring Data Rest