简体   繁体   中英

Error Occur when I try to split a sentence

I want to split a sentence into words when I try to split the sentence It show me an error, See this is my code:

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();
        }

    }    
}

When I try to execute the above code It show me an error like below

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

I couldn't find error in my code, Can you please tell me solution for the above problem.

while(it1.hasNext())
{
     Double db=(Double)it1.next();
     System.out.println(db);
}

Here:

while(it1.hasNext())
{
    Double db=(Double)it.next();
    System.out.println(db);
}

You want it to be Double db=(Double)it1.next() .

For the future, two rules to avoid errors like this:

  1. Use descriptive and meaningful variable names whenever possible (which is to say, pretty much, always)
  2. Do not create more variables than you need (for example, in this case, you do not need two iterator variables, could have just reused it). Better yet, limit the scope of variables to be as narrow as possible: do not do

     Iterator it = foo.iterator(); while(it.hasNext()) { ... } 

do this instead:

for(Iterator it = foo.iterator(); it.hasNext();) { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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