当我尝试拆分句子时,我想将一个句子分成单词它显示错误, 看到这是我的代码:
import java.io.*;
import java.util.*;
public class RunExe {
public static void main(String[] args) {
try
{
HashMap<String, String> map=new HashMap<String, String>();
ArrayList arrList=new ArrayList();
Process pr=Runtime.getRuntime().exec("VmgDiskActivityTest.exe");
BufferedReader rd=new BufferedReader(new InputStreamReader(pr.getInputStream()));
String lines=rd.readLine();
//String[] words=lines.split("=");
while(lines!=null)
{
String[] words=lines.split("=");
map.put(words[0], words[1]);
lines=rd.readLine();
}
Iterator it=map.entrySet().iterator();
while(it.hasNext())
{
Map.Entry str=(Map.Entry)it.next();
System.out.println("Keys :::"+str.getKey());
System.out.println("Values ::"+str.getValue());
String info=(String)str.getKey();
String values=(String)str.getValue();
String[] contentSplit=values.split("#");
for(String s : contentSplit)
{
//System.out.println(s);
arrList.add(Double.parseDouble(s));
}
}
Iterator it1=arrList.iterator();
while(it1.hasNext())
{
Double db=(Double)it.next();
System.out.println(db);
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
当我尝试执行上面的代码时,它向我显示如下错误
Keys :::0 C: D: E:
Values ::99.094152#0.480072#0.499131#0.004801#0.000294#0.009792#0.000000#1.000080#4096.328080#17.001362#217105.388231
Exception in thread "main" java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:897)
at java.util.HashMap$EntryIterator.next(HashMap.java:934)
at java.util.HashMap$EntryIterator.next(HashMap.java:932)
at RunExe.main(RunExe.java:40)
Keys :::_Total
Values ::99.094152#0.480072#0.499131#0.004801#0.000294#0.009792#0.000000#1.000080#4096.328080#17.001362#217105.388231
我的代码中找不到错误,请您告诉我上述问题的解决方案。
答案 0 :(得分:3)
while(it1.hasNext())
{
Double db=(Double)it1.next();
System.out.println(db);
}
答案 1 :(得分:3)
下面:
while(it1.hasNext())
{
Double db=(Double)it.next();
System.out.println(db);
}
您希望它为Double db=(Double)it1.next()
。
对于未来,有两条规则可以避免这样的错误:
不要创建超出需要的变量(例如,在这种情况下,您不需要两个迭代器变量,可以重新使用它)。更好的是,将变量的范围限制在尽可能窄的范围内:不要做
Iterator it = foo.iterator();
while(it.hasNext()) { ... }
改为:
for(Iterator it = foo.iterator(); it.hasNext();) { ... }