简体   繁体   English

没有这种元素异常扫描器

[英]no such element exception scanner

Scanner s = new Scanner(new File("src/mail_list"));    
while (s.hasNextLine()){
        String line1 = s.nextLine();
        if (line1.startsWith("Users")){
            line1 = s.nextLine();
            while (!(line1 = s.nextLine()).isEmpty()) {
                String arr[] = line1.split(" ", 4);
                users.add(new User(arr[0],arr[1],arr[2],arr[3]));
            }
        }
        if (line1.startsWith("Lists")){
            line1 = s.nextLine();
            while (!(line1 = s.nextLine()).isEmpty()) { //exception here
                String arr1[] = line1.split(" ", 2);
                ArrayList<String> tmp = new ArrayList<String>();
                StringTokenizer st = new StringTokenizer(arr1[1]);
                while (st.hasMoreTokens()) {
                    tmp.add(st.nextToken());
                }
                list.add(new List((arr1[0]), tmp));
            }
        }
    }

/*-testfile-start*/
Keyword: Users
username1 Name1 Surname1 email1
username2 Name2 Surname2 email2

Keyword: Lists
list_name username1 username2 ...
/*-testfile-end*/

I'm using the above code in order to sort things from the above testfile pattern. 我正在使用上面的代码来从上面的testfile模式中对事物进行排序。 Basically it means that if I encounter the keyword "Users" I have to add the said info about the user. 基本上它意味着如果我遇到关键字“用户”,我必须添加关于用户的所述信息。

I marked in the code where the exception rises. 我在代码中标记了异常上升的地方。 Any ideas on how to counter it? 关于如何应对它的任何想法?

You are calling nextLine() twice but only checking hasNextLine() once. 您正在调用nextLine()两次,但只检查hasNextLine()一次。

String line1 = s.nextLine();
    if (line1.startsWith("Users")){
        line1 = s.nextLine();

Meaning you are getting the next line without knowing if there is one, which throws the exception if there isn't one. 意思是你在不知道是否有一行的情况下获得下一行,如果没有,则抛出异常。

I found a stupid solution to it. 我找到了一个愚蠢的解决方案。 I just added a 'dummy' char 2 lines after the last line. 我刚刚在最后一行之后添加了一个'虚拟'字符2行。 and it works. 它的工作原理。 It's not a perfect solution but since the testfile is not meant to be seen by any1 i'll take it for now... Thank's everybody who brainstormed with me for this 45 mins. 这不是一个完美的解决方案,但由于测试文件不是任何人都可以看到的,我现在就把它拿走...感谢所有与我共谋45分钟的人。

Do!(line1 = s.nextLine())!= null,不为空,因为它无法读取空行。

From Scanner#nextLine : 来自Scanner#nextLine

Throws: 抛出:
NoSuchElementException - if no line was found. NoSuchElementException - 如果未找到任何行。

And you have this code: 你有这个代码:

while (s.hasNextLine()) {
    //checked if there's line to read
    String line1 = s.nextLine();
    if (line1.startsWith("Users")) {
        //not checked if there's line to read
        line1 = s.nextLine();
        //not checked here either
        while (!(line1 = s.nextLine()).isEmpty()) {
            String arr[] = line1.split(" ", 4);
            users.add(new User(arr[0],arr[1],arr[2],arr[3]));
        }
    }
    //similar in code below...
}

Make sure to validate there's a line to be read before using Scanner#nextLine . 确保在使用Scanner#nextLine之前验证是否有要读取的行。 Handle the exceptions accordingly. 相应地处理异常。

在此输入图像描述

As you can see, this method throws NoSuchElementException when no line was found. 如您所见,此方法在未找到任何行时抛出NoSuchElementException。

if (line1.startsWith("Lists")){
        line1 = s.nextLine(); // <=============== ?
        while (!(line1 = s.nextLine()).isEmpty()) { //exception here

How do you know that you have more lines there and in your comment line? 你怎么知道你的评论线上有更多的线?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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