使用FileReader和StringTokenizer时的NumberFormatException

时间:2017-01-31 22:21:08

标签: filereader tokenize numberformatexception

我遇到的问题是“Office”字符串。我得到这个:java.lang.NumberFormatException:对于输入字符串:“Office:”我觉得我需要在tokenizer部分使用parseline做其他事情吗?我是在正确的轨道上吗?我基本上试图从文件中读取,而不是计算总销售额并写入另一个文件。即使我尝试将people.txt文件显示到GUI上的输出屏幕,这也是我得到的错误。我只需要一点建议去哪里解决这个问题。我查了很多东西,但还没有找到。

public class PersonReader {

    public static void main(String args[]) throws IOException {
        PersonReader reader = new PersonReader();
        List<person> people = reader.readPeople("people.txt");
        System.out.println(people);

    }

    public List<person> readPeople(String filename) throws IOException {

        File f = new File(filename);
        FileReader reader = new FileReader(f);
        BufferedReader breader = new BufferedReader(reader);

        List<person> people = new ArrayList<person>();

        String line = breader.readLine();
        while (line != null) {
            person p = null;

            try {
                p = parseLine(line);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (p == null) {
                System.out.println("This row is bad." + line);
            } else {
                people.add(p);
            }
            line = breader.readLine();
        }

        return people;
    }

    private static person parseLine(String line) {

        int repID;
        String firstName;
        String lastName;
        double books;
        double paper;
        double office;
        String district;
        String contact;
        String next;

        StringTokenizer st = new StringTokenizer(line, ", ");

        repID = Integer.parseInt(st.nextToken().trim());
        firstName = st.nextToken().trim();
        lastName = st.nextToken().trim();
        books = Double.parseDouble(st.nextToken().trim());
        parseLine(line);
        paper = Double.parseDouble(st.nextToken().trim());
        parseLine(line);
        office = Double.parseDouble(st.nextToken().trim());
        parseLine(line);
        district = st.nextToken().trim();
        parseLine(line);
        contact = st.nextToken().trim();
        parseLine(line);


        if (repID < 1) {
            return null;
        }
        if (firstName.length() == 0) {
            return null;
        }
        if (lastName.length() == 0) {
            return null;
        }
        if (books < 1) {
            return null;
        }
        if (paper < 1) {
            return null;
        }
        if (office < 1) {
            return null;
        }
        if (district.length() == 0) {
            return null;
        }
        if (contact.length() == 0) {
            return null;
        }

        person p = new person();
        p.setRepID(repID);
        p.setFirstName(firstName);
        p.setLastName(lastName);
        p.setBooks(books);
        p.setPaper(paper);
        p.setOffice(office);
        p.setDistrict(district);
        p.setContact(contact);
        return p;

    }



}

0 个答案:

没有答案