Reputation: 1351
I have a text file full of numbers and I want to read the numbers into Java and then make a list that I can sort. it has been a while since I have used java and I am forgetting how to do this.
the text file looks something like this
4.5234
9.3564
1.2342
4.4674
9.6545
6.7856
Upvotes: 11
Views: 14064
Reputation: 93197
You can use a Scanner
on a File
and use the nextDouble()
or nextFloat()
method.
Scanner scanner = new Scanner(new File("pathToYourFile"));
List<Double> doubles = new ArrayList<Double>();
while(scanner.hasNextDouble()){
doubles.add(scanner.nextDouble());
}
Collections.sort(doubles);
Resources :
Upvotes: 11
Reputation: 39603
You do something like this.
EDIT: I've tried to implement the changes dbkk mentioned in his comments so the code actually will be correct. Tell me if I got something wrong.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadList {
public static void main(String[] args) throws IOException {
BufferedReader in = null;
FileReader fr = null;
List<Double> list = new ArrayList<Double>();
try {
fr = new FileReader("list.txt");
in = new BufferedReader(fr);
String str;
while ((str = in.readLine()) != null) {
list.add(Double.parseDouble(str));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
fr.close();
}
for (double d : list) System.out.println(d);
}
}
Upvotes: 3
Reputation: 23812
Use java.util.Scanner
:
public List<Doubles> getNumbers(String fileName) {
List<Double> numbers = new ArrayList<Double>();
Scanner sc = new Scanner(new File(fileName));
while (sc.hasNextDouble()) {
numbers.add(sc.nextDouble());
}
return numbers;
}
Upvotes: 1
Reputation: 299218
This is really fun and simple if you use Guava:
final File f = new File("your/file.txt");
final List<Float> listOfFloats =
Lists.transform(Files.readLines(f, Charset.defaultCharset()),
new Function<String, Float>(){
@Override
public Float apply(final String from){
return Float.valueOf(from);
}
});
And here's a similar version using Apache Commons / IO:
final File f = new File("your/file.txt");
final List<String> lines = FileUtils.readLines(f);
final List<Float> listOfFloats = new ArrayList<Float>(lines.size());
for(final String line : lines){
listOfFloats.add(Float.valueOf(line));
}
Upvotes: 2
Reputation: 1024
Not accounting for the fact that you need to setup exception handling and such based on your sourrounding code, it'll look something like this:
BufferedReader br = new BufferedReader(new FileReader(new File("filename.txt")));
ArrayList<Double> ald = new ArrayList<Double>();
String line;
while(true)
{
line = br.readLine();
if(line == null) break;
ald.add(new Double(line));
}
Upvotes: 1
Reputation: 22025
Short instructions without code:
BufferedReader
to read from your file.reader.readLine()
, until readLine()
returns null
(== end of file)Float
or Double
(e.g. Float.valueOf(line)
or Double.valueOf(line)
Float
or Double
objects to an ArrayList
.Upvotes: 1