这是我的代码:
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();}
}
我正在解析的输出看起来像这样:
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
--
错误出现在两行
"strength = Integer.parseInt(info2[2]);"
和"kanali = Integer.parseInt(info1[3]);"
...我似乎无法弄清楚问题出在哪里。当我分割字符串时,根据输出,我想要的信息在第二和第三字段中。那么,为什么要尝试传递null
字符串进行整数解析呢?堆栈跟踪:
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)
最佳答案
线
Quality=2/100 Signal level=-89 dBm Noise level=-92 dBm
包含双精度空格,因此在
split
结果中,您有空的String
,非空的String
不在您认为的索引处。用索引输出打印出
"Quality=2/100 Signal level=-89 dBm Noise level=-92 dBm".split(" ");
的结果0: Quality=2/100
1:
2: Signal
3: level=-89
4: dBm
5:
6: Noise
7: level=-92
8: dBm
您的文件中似乎有前导空格,因此带有非空
String
的索引将在之后出现。