简体   繁体   English

为什么在将字符串转换为整数时会出现NumberFormatException?

[英]Why am I getting NumberFormatException while converting a string to integer?

This is my code: 这是我的代码:

 public void setAreaAccessPoints(){
    String mac = "",essid = "",status = "";
    int strength = 0,kanali = 0;
    List<String> AccessPoints = new ArrayList<String>(); //i lista me ta access points
    String temp;
    try{
        String[] command = {"/bin/sh", "-c", "sudo iwlist " + wirelessName + " scanning | grep -A5 \"Cell\" "};
        Process child = Runtime.getRuntime().exec(command);
        BufferedReader r = new BufferedReader(new InputStreamReader(child.getInputStream()));
        while((temp = r.readLine()) != null){
            if(temp.contains("Cell")){
                String[] info = temp.split(" ");
                mac = info[3];
                System.out.println(mac);
                do{
                    temp = r.readLine();
                    if(temp.contains("ESSID:")){
                        essid = temp.replace("ESSID:","");
                    }
                    if(temp.contains("Frequency:")){
                        String[] info1 = temp.split(" ");
                        info1[3] = info1[3].replace(")","");
                        kanali = Integer.parseInt(info1[3]);
                    }
                    if(temp.contains("Mode:")){
                        status = temp.replace("Mode:","");
                    }
                    if(temp.contains("Quality=")){
                        String[] info2 = temp.split(" ");
                        info2[3] = info2[3].replace("level=","");
                        strength = Integer.parseInt(info2[3]);
                    }
                    if(temp.contains("Protocol:")){
                        temp = r.readLine();
                    }
                }while(!(temp.contains("Cell")));
                AccessPoint newAP = new AccessPoint(mac,essid,kanali,status,strength); 
                AccessPoints.add(newAP.toString());  //vazoume ta access points sti lista san strings
            }
        }
        r.close();
        for(String s : AccessPoints)
            System.out.println(s);
    }catch(IOException e){e.printStackTrace();}
}

The output which i am parsing looks like this: 我正在解析的输出如下所示:

      Cell 04 - Address: 00:05:59:30:C1:7C
                Protocol:802.11b/g
                ESSID:"NA home"
                Mode:Managed
                Frequency:2.437 GHz (Channel 6)
                Quality=2/100  Signal level=-89 dBm  Noise level=-92 dBm
  --
      Cell 05 - Address: 00:05:59:43:AE:C9
                Protocol:802.11b/g
                ESSID:"NetFasteR IAD 2 (PSTN)"
                Mode:Managed
                Frequency:2.437 GHz (Channel 6)
                Quality=0/100  Signal level=-91 dBm  Noise level=-94 dBm
  --
      Cell 06 - Address: 00:05:59:3B:C1:FA
                Protocol:802.11b/g
                ESSID:"Kpanagiotou"
                Mode:Managed
                Frequency:2.437 GHz (Channel 6)
                Quality=0/100  Signal level=-91 dBm  Noise level=-94 dBm
  --

The error is in the 2 lines "strength = Integer.parseInt(info2[2]);" 错误在2行"strength = Integer.parseInt(info2[2]);" and "kanali = Integer.parseInt(info1[3]);" "kanali = Integer.parseInt(info1[3]);" ... I can't seem to figure out where is the problem. ......我似乎无法弄清问题在哪里。 When I am splitting the string, the info I want is in the second and third field according to the output. 当我分割字符串时,我想要的信息是根据输出在第二个和第三个字段中。 So why does it try to pass a null string for integer parsing? 那么为什么它会尝试传递null字符串进行整数解析呢?

StackTrace: 堆栈跟踪:

 Exception in thread "Thread-1" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at askisi1.Wireless.setAreaAccessPoints(Wireless.java:213)
    at askisi1.Wireless.run(Wireless.java:43)
    at java.lang.Thread.run(Thread.java:722)

you are checking for something and replacing something else... 你正在检查某些东西并替换其他东西......

 if(temp.contains("Quality=")){
                    String[] info2 = temp.split(" ");
                    info2[1] = info2[1].replace("level=","");
                    strength = Integer.parseInt(info2[2]);
                }

The line 这条线

Quality=2/100  Signal level=-89 dBm  Noise level=-92 dBm

contains double spaces, so in the split result, you have empty String s and the non-empty String s are not at the indices you think they are. 包含双重空格,因此在split结果中,您有空String ,非空String不在您认为的索引处。

Printing out the result of "Quality=2/100 Signal level=-89 dBm Noise level=-92 dBm".split(" "); 打印出"Quality=2/100 Signal level=-89 dBm Noise level=-92 dBm".split(" "); with indices yields 指数收益率

0: Quality=2/100
1: 
2: Signal
3: level=-89
4: dBm
5: 
6: Noise
7: level=-92
8: dBm

You seem to have leading spaces in your file, so the indices with nonempty String s would be later then. 您似乎在文件中有前导空格,因此具有非空String s的索引将在稍后。

Since you have spaces in-front of the lines you are getting wrong part for to format without any spaces in both sides your logic is working 由于线条前面有空格,因此格式错误的部分没有任何空格,你的逻辑工作正常

you could do trim() and check your result 你可以修剪()并检查你的结果

 temp = r.readLine().trim();

For example this is working 例如,这是有效的

String str ="Frequency:2.437 GHz (Channel 6)";
String [] str1 = str.split(" ");
System.out.println(Integer.parseInt(str1[3].replace(")", "")));

You remove "level=" from info2[1] and then don't do anything with it later. 你从info2 [1]中删除“level =”,然后再对它做任何事情。 You should remove it from info2[2]. 你应该从info2 [2]中删除它。 Also, note that they are separated by double spaces as noted in another answer here. 另外,请注意它们被双倍空格分隔,如此处的另一个答案中所述。

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

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