I'm working on this school project and was wondering if there was any way of storing this information better, faster, more professionally. I'm also restricted to only using an array; don't ask me we are not allowed to use ArrayLists yet.
My Code:
public void loadTweets(String fileName){
try {
File file = new File(fileName);
Scanner s = new Scanner(file);
while(s.hasNextLine()){
numberOfTweets++;
s.nextLine();
}
tweets = new String[numberOfTweets];
s.close();
s = new Scanner(file);
int counter = 0;
while(s.hasNextLine()){
String[] elements = s.nextLine().split("\t");
tweets[counter] = elements[2];
counter++;
}
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
File Example:
Each field is separated by a tab and it goes, user > date posted > tweet.
USER_989b85bb 2010-03-04T15:34:46 @USER_6921e61d can I be... USER_989b85bb 2010-03-04T15:34:47 superstar USER_a75657c2 2010-03-03T00:02:54 @USER_13e8a102 They reached a USER_a75657c2 2010-03-07T21:45:48 So SunChips made a bag... USER_ee551c6c 2010-03-07T15:40:27 drthema: Do something today that USER_6c78461b 2010-03-03T05:13:34 @USER_a3d59856 yes, i watched... USER_92b2293c 2010-03-04T14:00:11 RT @USER_5aac9e88: Let no 1 push u USER_75c62ed9 2010-03-07T03:35:38 @USER_cb237f7f Congrats on...
Files.lines().map(l -> String.split("\t")).map(l -> l[2]).toArray(String[]::new)
doesn't strictly speaking use aList
of any kind - no more so thanScanner
does. Probably goes against the spirit of the question though... – Boris the Spider Feb 4 at 11:10