Hi guys.
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package daos; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import model.City; import model.YearData; import repositories.Repository; /** * * @author mga */ public class DAOTextImpl implements DAOInterface { private ArrayList<YearData>yeardata; static final char DELIMITER=','; @Override public Repository load(String filename) { Repository repository = new Repository(); try (BufferedReader br = new BufferedReader(new FileReader(filename))) { String[] temp; String line = br.readLine(); while(line!=null){ temp=line.split(Character.toString(DELIMITER)); int id = Integer.parseInt(temp[0]); String cityName = stripQuotes(temp[1]); String country=stripQuotes(temp[2]); City city=new City (id, cityName, country); /* line=br.readLine(); temp=line.split(Character.toString(DELIMITER)); String year=stripQuotes(temp[4]); float precipitation=Float.parseFloat(temp[5]); int maxTemp=Integer.parseInt(temp[6]); int minTemp=Integer.parseInt(temp[7]); int windSpeed=Integer.parseInt(temp[8]); String windDirection=stripQuotes(temp[9]); YearData yeardata=new YearData(year, precipitation, maxTemp, minTemp,windSpeed, windDirection); */ repository.getItem(id); // repository.add(city); line = br.readLine(); } br.close(); } catch (IOException ex) { Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex); } return repository; } @Override public void store(String filename, Repository repository) { try (PrintWriter output = new PrintWriter(filename)) { output.print(repository.toString()); output.close(); } catch (FileNotFoundException ex) { Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex); } } private String stripQuotes(String str) { return str.substring(1, str.length()-1); } }
I am having problems with the above code. Whenever I run the code, all I get is:
[] I suspect that this is due to the fact that the array is empty....i.e. is not being read properly and so returns its default value, i.e. NULL.
Whenever I try and open a file, I get NumberFormatException. This, I suspect is due to the fact that it contains illegal characters, such as a date, floats etc.
I am also unsure as to how I would utilise an arraylist of objects, I have two (model) classes. One class contains an ArrayList, the second class contains the member variables which are to populate the ArrayList. However, I am utterly baffled as to what I do to implement the ArrayList with the code above. I cannot convert it to String, so this means that using something like:
For example, won't work due to the mismatch due to string/object.ArrayList<String> result = new ArrayList<>();
try (FileReader f = new FileReader(filename)) {
StringBuffer sb = new StringBuffer();
while (f.ready()) {
char c = (char) f.read();
if (c == '\n') {
result.add(sb.toString());
sb = new StringBuffer();
} else {
sb.append(c);
}
}
if (sb.length() > 0) {
result.add(sb.toString());
}
}
return result;
Cab anyone please advise me? I'm totally stuck