[英]method throws NumberFormatException where it shouldnt, worked normally so far
當我轉換的字符串是int時,我似乎無法弄清楚為什么它會拋出NFE
[碼]
public void setCurrentTransferRate(){
try{
long startTime = System.currentTimeMillis();
String[] command = {"/bin/sh", "-c", "ifconfig " + interface_name + " | grep -oP 'RX bytes:[0-9]{1,11}'"};
String[] command1 = {"/bin/sh", "-c", "ifconfig " + interface_name + " | grep -oP 'TX bytes:[0-9]{1,11}'"};
Process child = Runtime.getRuntime().exec(command);
Process child1 = Runtime.getRuntime().exec(command1);
BufferedReader r = new BufferedReader(new InputStreamReader(child.getInputStream())); //i prwti metrisi twn RX kai TX bytes
BufferedReader r1 = new BufferedReader(new InputStreamReader(child1.getInputStream()));
String temp = r.readLine();
temp = temp.replace("RX bytes:","");
String temp1 = r1.readLine();
temp1 = temp1.replace("TX bytes:","");
r.close();
r1.close();
int x = Integer.parseInt(temp);
int y = Integer.parseInt(temp1);
}catch(IOException e){e.printStackTrace();}
catch(InterruptedException e){e.printStackTrace();}
}[/code]
產生錯誤的字符串是temp
我得到了錯誤
[碼]
Exception in thread "Thread-0" java.lang.NumberFormatException: For input string: "3262469144"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)
at java.lang.Integer.parseInt(Integer.java:527)
at askisi1.General.setCurrentTransferRate(General.java:187)
at askisi1.General.<init>(General.java:27)
at askisi1.mainThread.run(mainThread.java:17)
at java.lang.Thread.run(Thread.java:722)[/code]
我真的可以用一雙新鮮的眼睛
整數的最大正值是Integer.MAX_VALUE
,即2,147,483,647
。 你可以用一個長的代替:
long x = Long.parseLong(temp);
它應解決您的問題(如果您的所有數字都小於Long.MAX_VALUE
或9,223,372,036,854,775,807
)。
3262469144對於int來說太大了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.