Reputation: 547
I have a text file with integers and double number like this:
16 -122.454803 41.923870
17 -122.440536 41.946377
18 -122.440498 41.956013
I have 3 lists(one int and two double lists) and i want to save the items from each column. How can i split the items and save them in the lists?
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Double> list2 = new ArrayList<Double>();
ArrayList<Double> list3 = new ArrayList<Double>();
BufferedReader in = new BufferedReader(new FileReader("Nodes.txt"));
String line;
while((line = in.readLine()) != null){
list1.setNodeId(line.split(" "));
System.out.println(line);
}
in.close();
Upvotes: 1
Views: 64
Reputation: 54148
You can try by this way ;)
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Double> list2 = new ArrayList<Double>();
ArrayList<Double> list3 = new ArrayList<Double>();
Path path = Paths.get("C:/User/.../.../.../nodes.txt");
Scanner in = new Scanner(path);
double line;
while(in.hasNextDouble()){
line = in.nextDouble();
list1.add((int)line);
line = in.nextDouble();
list2.add(line);
line = in.nextDouble();
list3.add(line);
System.out.println(line);
}
in.close();
Don't forget to write the correct path, and tell if it's working ;)
edit : be careful, i tried and "15.05" is not working with my method, it needs "15,05", i think it will be the same with the post just over mine
Upvotes: 0
Reputation: 18923
You can do something like this:
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Double> list2 = new ArrayList<Double>();
ArrayList<Double> list3 = new ArrayList<Double>();
BufferedReader in;
String line;
try {
in = new BufferedReader(new FileReader("Nodes.txt"));
while((line = in.readLine()) != null){
System.out.println(line);
String arr[] = line.split(" ");
list1.add(Integer.valueOf(arr[0]));
list2.add(Double.valueOf(arr[1]));
list3.add(Double.valueOf(arr[2]));
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < list1.size(); i++){
System.out.println(list1.get(i));
}
for (int i = 0; i < list2.size(); i++){
System.out.println(list2.get(i));
}
for (int i = 0; i < list3.size(); i++){
System.out.println(list3.get(i));
}
}
Upvotes: 2
Reputation: 3312
String.split returns an array of Strings. You need to take appropriate array entry and convert it to desired value type
String[] entries = line.split(" ");
list1.add(Integer.valueOf(entries[0]));
list2.add(Double.valueOf(entries[1]));
list3.add(Double.valueOf(entries[2]));
Upvotes: 1
Reputation: 2319
One way to process the data:
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(" ");
if (parts.length != 3) {
continue;
}
list1.add(Integer.parseInt(parts[0]));
list2.add(Double.parseDouble(parts[1]));
list3.add(Double.parseDouble(parts[2]));
}
However: this code lacks error checking on the data type. And I guess you need to process the data afterwards, so I highly suggest that you write a GeoPosition
class (just a name guess from the data) with the index and position fields and a parse(String)
method to parse each line. A list of such elements is much easier to process afterwards in comparison to 3 separate lists.
Upvotes: 2