A file name will be passed in from standard in. I want to open it, read it, and create some information based off the text in the file.
For example, if this is a line in the file:
Hristo 3
... then I want to create a Member()
named Hristo with a value of 3. So I want to pull out a String
for the name and an int
for the value. The name and the value are separated by some unknown number of tabs and spaces which I need to ignore. Could I just read the line, use .trim() to get rid of whitespace, and the last character would be the value?
I have not shown the class Member() for simplicity's sake. This is what I have so far:
public static void main(String[] args) {
int numMembers = 0;
ArrayList<Member> veteranMembers = new ArrayList<Member>();
File file = new File(args[0]);
FileReader fr;
BufferedReader br;
// attempt to open and read file
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String line;
// read file
while ((line = br.readLine()) != null) {
// extract name and value from line
... ? ...
// create member
// initialize name and value
Member member = new Member();
veteranMembers.add(member);
}
br.close();
} catch (FileNotFoundException e1) {
// Unable to find file.
e1.printStackTrace();
} catch (IOException e) {
// Unable to read line.
e.printStackTrace();
}
}
How would I parse that line of text?
Thanks in advance for your help.