简体   繁体   English

StringTokenizer“ NoExploranceException”

[英]StringTokenizer “NoSuchelementException”

So For some reason i'm getting a java.lang.Exception: java.util.NoSuchElementException. 所以由于某种原因,我得到了一个java.lang.Exception:java.util.NoSuchElementException。 The values are priting off like this. 值在像这样引爆。

back
bison
1.0
back
dog
3.0
becaue
bison
1.0
best
bison
1.0
david
bison
1.0
ever
bison
1.0
i
bison
1.0
i
dog
4.0
im
bison
1.0
is
bison
1.0

But at the very end i get the NoSuchelementException: 但是最后我得到了NozhilementException:

my code is simple: 我的代码很简单:

StringTokenizer line = new StringTokenizer(new String(value.getBytes()));
    while (line.hasMoreElements()){
        Text unWord = new Text((String) line.nextElement());
        String unAuth = (String) line.nextElement();
        float unVal = Float.parseFloat(line.nextToken());

        System.out.println(unWord);
        System.out.println(unAuth);
        System.out.println(unVal);
    }

I can't seem to figure out why the Elements would print off, but i would get a noSuchelementException at the end. 我似乎无法弄清楚为什么会打印出Elements,但是最后我会得到一个noCHIlementException。

The problem is that you call hasMoreElements() once per iteration, but you call nextElement / nextToken three times. 问题是您每次迭代都调用一次hasMoreElements() ,但是又调用了一次nextElement / nextToken 3次。 This is error-prone. 这容易出错。 Even if the file follows the exact format, such as in your case, there is a possibility of having a blank line at the end. 即使文件遵循正确的格式(例如您的情况),也可能在末尾有一个空行。

It looks like that is precisely what is happening in your case: when you ask if the tokenizer has more elements, you get true , but there is only one element, which happens to be a new line character. 看起来这正是您的情况:当您问问令牌生成器是否包含更多元素时,您会得到true ,但是只有一个元素恰好是换行符。 The first call to nextElement succeeds, but the second one fails. 第一次调用nextElement成功,但是第二次失败。

To fix this problem add hasMoreElements() check before each call to nextElement , and quit if there is no next element: 要解决此问题,请在每次调用nextElement之前添加hasMoreElements()检查,如果没有下一个元素,则退出:

Text unWord = new Text((String) line.nextElement());
if (!line.hasMoreElements()) break;
String unAuth = (String) line.nextElement();
if (!line.hasMoreElements()) break;
float unVal = Float.parseFloat(line.nextToken());

Well you call line.nextElement() twice while only checking for line.hasMoreElements() once. 好吧,您两次调用line.nextElement() ,而只检查一次line.hasMoreElements() Could it be that there's only one remaining element at the end, not two? 可能是最后只剩下一个元素,而不是两个吗?

On each iteration of your loop, you test once whether there are any more tokens available, and then attempt to draw three tokens from the tokenizer. 在循环的每次迭代中,只需测试一次是否还有可用的令牌,然后尝试从令牌生成器中提取三个令牌。 You print the results if all three tokens are successfully drawn, but you can be confident only of one being successfully drawn. 如果成功绘制了所有三个标记,则可以打印结果,但是您可以确信只有一个成功绘制了标记。 If the total number of tokens in the original string is not a multiple of three then you will certainly get a NoSuchElementException . 如果原始字符串中的令牌总数不是三的倍数,那么您肯定会得到NoSuchElementException If you think your data otherwise look as you expect, then that might result from trailing whitespace in your original string (yielding an unexpected empty token at the end). 如果您认为自己的数据看起来像您期望的那样,则可能是由于在原始字符串中尾随空格引起的(最后产生一个意外的空令牌)。

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

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