tags:

views:

45

answers:

4

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.

A: 

Looks like a home work. You came really close doing it. Use StringTokenizer to tokenize the line. Then create a new member object and and call setters for both the attributes with tokens as params. If your second attribute is an int use parseInt to convert and assign it.

Teja Kantamneni
Its not homework... its actually a Facebook puzzle I'm trying to tackle. Also, can you post some code... I've never used `StringTokenizer`.
Hristo
+3  A: 

I would use the split function. You can give it a regular expression as the argument i.e.

line.split(" |\t");

will return array of the words ( {list[0] = Hristo, list[1] = 3} in your example) Hope it helps.

Klark
Ahh great. I just looked into split right before you answered. The only problem here is the potential split into many elements. So it potentially won't be just 2 with the Name and the Value, but more with empty value. How would I overcome that... just check if " "?
Hristo
I just tried it.This is going to do the job:split("\\s+");
Klark
Yup. That does the job. Thanks!
Hristo
+2  A: 

Use split("\\s+"), this regex ignore any space, tab, etc from the String.

eiefai
I don't exactly understand how regex works... what is "\\s+" saying?
Hristo
It says "ignore all spaces" from the string to split
eiefai
Cool. Where did you get this from? Is there like a java package of regex expressions somewhere?
Hristo
I don't know, I like regex and I'm trying to learn them :)
eiefai
fair enough. Thanks!
Hristo
+2  A: 

A more robust way might be to use regular expressions; if you received malformed input (e.g., "Ted One"), parseInt() would throw a NumberFormatException.

import java.util.regex.*;

...

Pattern p = Pattern.compile("^(.*)\\s+(\\d+)$"); // Create a regex Pattern that only matches (text - white space - integer)
Matcher m = p.matcher(line); // Create a Matcher to test the input line
if(m.find()){
      // If there's a match, ..
    String name = m.group(1); // Set "name" to the first parenthesized group
    String value = m.group(2); // Set "value" to the second parenthesized group
}
else{
      // Bad Input
}
godheadatomic