简体   繁体   中英

how do I extract Name and Value from a line of text while reading from a file using Java?

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.

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

line.split(" |\t");

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

使用split("\\\\s+") ,此正则表达式忽略字符串中的任何空格,制表符等。

A more robust way might be to use regular expressions; if you received malformed input (eg, "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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM