I want to write A Star Algorithm by java program and I want to read the distance from the Text file like this
89 R A
118 A T
140 M S
85 B U
As you see in my text file I have three columns but with this code that I wrote it will give me just two columns but I want to read all of my columns which is three columns as you see in above
List<String> halist = new ArrayList<String>();
File f = new File("mapfile.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while ( (record=dis.readLine()) != null ) {
Map<Integer, String> hamap = new HashMap<Integer, String>();
String[] columns = record.split(" ");
hamap.put(Integer.valueOf(columns[0]), columns[1]);
for(Map.Entry<Integer,String> m :hamap.entrySet()) {
System.out.println(m.getKey()+" "+m.getValue());
}
}
You never use the third column
hamap.put(Integer.valueOf(columns[0]), columns[1] +" " + columns[2]);
Or you can use Lists of Lists:
Map<Integer, List<String>> hamap = new HashMap<Integer, List<String>>();
String[] columns = record.split(" ");
List<String> otherColumns = new ArrayList<String>();
for (int i=1; i < columns.length; i++) {
otherColumns.add(columns[i]);
}
hamap.put(Integer.valueOf(columns[0]), otherColumns);
for(Map.Entry<Integer,List<String>> m :hamap.entrySet()) {
System.out.println(m.getKey()+" "+m.getValue());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With