Reputation: 179
I have a problem with something like that. Unfortunately, I can't edit anything.
en_GB United States 2015-07-10 2015-08-30 mountains 5,400.20 USD
This is one line from text file. I'm trying to prepare scanner for it and basing on it create object. Here what i made and what i can edit.
String lang;
String country;
Date[] dates; //contains two dates - arrival and departure
String place;
Double price;
String curr;
public TravelData(String lang, String countryIn, Date[] dateIn, String placeIn, Double priceIn, String currIn)
This is how my constructor looks like. I was trying to use scanner and next() method, but looks like it doesn't work with date. What would be the best way to cut this line of text on smaller parts?
My not working scanner :).
public void ReadOffers(File dataDir) throws FileNotFoundException{
TravelData travel;
for (final File fileEntry : dataDir.listFiles()) {
scan = new Scanner(fileEntry);
while(scan.hasNextLine()){
String l = scan.next();
String c = scan.next();
Date[] d = [Date.parse(scan.next()); Date.parse(scan.next())];
String pl = scan.next();
Double pr = Double.parseDouble(scan.next());
String cu = scan.next();
travel = new TravelData(l, c, d, pl, pr, cu);
travelList.add(travel);
}
}
}
Upvotes: 0
Views: 338
Reputation: 743
You can use BufferedReader instead to read the line and then split() and put it into an array of strings like this:
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileEntry)));
String line;
while(line = br.readNext() != null)
{
String[] words = line.split(" ");//assuming you want to split based on the whitespace char
//do what you want to do with the words
}//reapeat as long as there is a line to the file
Upvotes: 3
Reputation: 1124
Load it as an entire string
String fileContent = new String(readAllBytes(get("test.txt"));
Separate via regex using split(regex)
String[] separatedLines = fileContent.split("\n");
foreach(String s:separatedLines){
s.split(" ");
[parse as you wish, like you were doing]
[do stuff, put results in a list or in another array]
}
EDIT: I read that those are tabs: split("\t*"). EDIT2: as for the date, check SimpleDataFormat
Upvotes: 2