java中使用StringTokenizer时出现java.util.NoSuchElementException

时间:2015-06-07 02:35:34

标签: java arraylist stringtokenizer

我最近在文件读取,编写和比较核心java中的字符串方面做了简单的项目。我已经使用StringTokenizer来解析行中的每个元素。但在这样做时,我遇到以下错误消息。 有任何帮助,请提前感谢。 错误讯息:

Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at java.util.StringTokenizer.nextElement(Unknown Source)
at com.dhiraj.demo.ComparatorClass.main(ComparatorClass.java:64)

我的代码如下;

String listtable = "listtable.dat";
    ArrayList<ListTableClass> ltc = new ArrayList<ListTableClass>();

    try(FileInputStream fis = new FileInputStream(listtable);
            BufferedReader bio = new BufferedReader(new InputStreamReader(fis))){
        String line;
        while((line = bio.readLine())!=null){
            System.out.println(line);
            StringTokenizer to = new StringTokenizer(line, "|");
            //while(tokens.hasMoreElements()){
                //System.out.println(tokens.nextElement());

                //Integer id = Integer.parseInt(to.nextElement().toString().trim());
            //  System.out.println(id);
                Integer id = Integer.parseInt(to.nextElement().toString());

                //int id = Integer.parseInt(to.nextElement().toString().trim());
                System.out.println("i am error");
                String fname = to.nextElement().toString().trim();
                String lname = to.nextElement().toString().trim();
                String address = to.nextElement().toString().trim();
                Integer age = Integer.parseInt(to.nextElement().toString().trim());


                ltc.add(new ListTableClass(id, fname, lname, address, age));

            //}
        }
    }catch(IOException e){
        System.out.println(e);

    }       
    for(ListTableClass t: ltc){
        System.out.println(t.getId());
        System.out.println(t.getFname());
        System.out.println(t.getLname());   
        System.out.println(t.getAddress());     
        System.out.println(t.getAge());         

    }

和文件样本是:

11120|bijaya|khanal|biratnagar|25
11121|chandramani|sapkota|hetauda|25
11117|Hari|Sapkota|bhaktapur|25
11118|pramod|chaulagain|banepa|25
11119|bigyan|shrestha|birjung|25
11120|bijaya|khanal|biratnagar|25
11114|suman|hamal|shangrila|25
11115|kishor|Neupane|Sanepa|25

1 个答案:

答案 0 :(得分:1)

我首先将toString添加到ListTableClass,例如

public String toString() {
    return String.format("Id: %d%n" + //
            "Name (lname, fname): %s, %s%n" + //
            "Address: %s%n" + //
            "Age: %d",//
            id, lname, fname, address, age//
            );
}

然后我更喜欢String.split(String)Scanner并从用户的主文件夹中读取文件,最后编程到List界面(并使用带有<>的钻石运算符toString),你应该检查像

这样的emtpy行
public static void main(String[] args) {
    String listtable = "listtable.dat";
    List<ListTableClass> ltc = new ArrayList<>();
    File f = new File(System.getProperty("user.home"), listtable);
    try (Scanner s = new Scanner(f)) {
        while (s.hasNextLine()) {
            String line = s.nextLine();
            if (line.trim().isEmpty()) {
                continue;
            }
            System.out.println(line);
            String[] to = line.split("|");
            Integer id = Integer.parseInt(to[0].trim());
            String fname = to[1].trim();
            String lname = to[2].trim();
            String address = to[3].trim();
            Integer age = Integer.parseInt(to[4].trim());
            ltc.add(new ListTableClass(id, fname, lname, address, age));
        }
    } catch (IOException e) {
        System.out.println("Caught Exception: " + e.getMessage());
        e.printStackTrace();
    }
    for (ListTableClass t : ltc) {
        System.out.println(t);
    }
}