简体   繁体   English

管理NoSuchElementException StringTokenizer

[英]Managing NoSuchElementException StringTokenizer

I would like to manage the StringTokenizer NoSuchElementException when the username or password fields is not shown but I can not manage it. 当用户名或密码字段未显示但我无法管理时,我想管理StringTokenizer NoSuchElementException

final StringTokenizer tokenizer = new StringTokenizer(
        usernameAndPassword, ":");

System.out.println(usernameAndPassword);

while (tokenizer.hasMoreTokens()) {
    String tmp1 = tokenizer.nextToken();

    if (tokenizer.nextToken() == null) {
        System.out.println("pas d'username");
        username = "";
    } else {
        username = tmp1;
    }

    String tmp2 = tokenizer.nextToken();

    if (tokenizer.nextToken() == null) {
        System.out.println("pas de password");
        password = "";
    } else {
        password = tmp2;
    }
}

Thanks. 谢谢。

You can't just do if (tokenizer.nextToken() == null) directly before checking if it has more tokens or not. 您不能只是在检查它是否具有更多令牌之前直接执行if (tokenizer.nextToken() == null)

What do you want to check using the if condition? 您要使用if条件检查什么? Shouldn't it be if (StringUtils.isEmpty(tmp2)) instead of if (tokenizer.nextToken() == null) ? 应该不是if (StringUtils.isEmpty(tmp2))而不是if (tokenizer.nextToken() == null)吗?

If you want to scan next set of tokens then you'll have to do something like if(tokenizer.hasMoreTokens() && tokenizer.nextToken() != null) instead of just doing tokenizer.nextToken() as you don't know whether it has more tokens or not. 如果要扫描下一组令牌,则必须做类似if(tokenizer.hasMoreTokens() && tokenizer.nextToken() != null)之类的事情,而不是像不知道那样只做tokenizer.nextToken()是否具有更多令牌。

You can try adding the catch block. 您可以尝试添加catch块。

try {
    final StringTokenizer tokenizer = new StringTokenizer(
            usernameAndPassword, ":");

    System.out.println(usernameAndPassword);

    while(tokenizer.hasMoreTokens()){
        String tmp1 = tokenizer.nextToken();

        if (tmp1 == null){
            System.out.println("pas d'username");
            username = "";
        }
        else{
            username = tmp1;
        }

        String tmp2 = tokenizer.nextToken();

        if (tmp2 == null){
            System.out.println("pas de password");
            password = "";
        }
        else{
            password = tmp2;
        }
    }

} catch (NoSuchElementException e) {
    password = "";
}

Use this instead : 使用它代替:

final StringTokenizer tokenizer = new StringTokenizer(
        usernameAndPassword, ":");

System.out.println(usernameAndPassword);

while(tokenizer.hasMoreTokens()){
    String tmp1 = tokenizer.nextToken();

    if (tmp1 == null){
        System.out.println("pas d'username");
        username = "";
    }
    else{
        username = tmp1;
    }

    String tmp2 = tokenizer.nextToken();

    if (tmp2 == null){
        System.out.println("pas de password");
        password = "";
    }
    else{
        password = tmp2;
    }
}

When you intiliaze your string tokenizer it splits the string with the colon. 当您插入字符串标记器时,它将用冒号将字符串分割开。 So suppose you have a string A:B, it will get tokenised as A and B. You can fetch the value A and B using the nextToken() method. 因此,假设您有一个字符串A:B,它将被标记为A和B。您可以使用nextToken()方法获取值A和B。 Whenever you call this method the pointer goes to the next element. 无论何时调用此方法,指针都将指向下一个元素。 In the above code you are using this method 4 times. 在上面的代码中,您使用此方法4次。 After 2 time the pointer tries to fetch a value but does not succeed and hence the exception. 2次后,指针尝试获取一个值,但没有成功,因此引发异常。

Thanks 谢谢

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

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