Android 从文本文件解析数字时出现 Invalid Int 错误
Android Invalid Int error when parsing number from text file
我的网站上有一个包含数字(在本例中为 3)的 .txt,我使用此代码检查此数字是大于还是小于另一个数字,但代码给出了以下错误:
03-03 16:27:43.734: E/AndroidRuntime(16318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.downloadingprogressbar/com.example.downloadingprogressbar.MainActivity}: java.lang.NumberFormatException: Invalid int: "3
这是我的代码:
HttpGet httppost = new HttpGet("http://mywebsite.org/version.txt");
HttpResponse response;
try {
response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line + "\n");
}
String casa = new String(total.toString());
//boolean version = (casa>4);
if (Integer.parseInt(casa)>4){
risposta.setText("la tua versione è aggiornata");
}
else {
risposta.setText("aggiorna la tua versione");
}
试试这个删除可能围绕您的号码的所有空格:
if (Integer.parseInt(casa.trim()) > 4){
// ...
}
我同意 Kon 的观点,您的变量 "casa" 包含其他字符。
尝试使用trim()
方法:
if (Integer.parseInt(casa.trim())>4){
...
...
...
但现在我看到你在 total 变量中附加了“\n”,这个 "new line" 是必要的吗?:
while ((line = r.readLine()) != null) {
total.append(line);
}
我的网站上有一个包含数字(在本例中为 3)的 .txt,我使用此代码检查此数字是大于还是小于另一个数字,但代码给出了以下错误:
03-03 16:27:43.734: E/AndroidRuntime(16318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.downloadingprogressbar/com.example.downloadingprogressbar.MainActivity}: java.lang.NumberFormatException: Invalid int: "3
这是我的代码:
HttpGet httppost = new HttpGet("http://mywebsite.org/version.txt");
HttpResponse response;
try {
response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line + "\n");
}
String casa = new String(total.toString());
//boolean version = (casa>4);
if (Integer.parseInt(casa)>4){
risposta.setText("la tua versione è aggiornata");
}
else {
risposta.setText("aggiorna la tua versione");
}
试试这个删除可能围绕您的号码的所有空格:
if (Integer.parseInt(casa.trim()) > 4){
// ...
}
我同意 Kon 的观点,您的变量 "casa" 包含其他字符。
尝试使用trim()
方法:
if (Integer.parseInt(casa.trim())>4){
...
...
...
但现在我看到你在 total 变量中附加了“\n”,这个 "new line" 是必要的吗?:
while ((line = r.readLine()) != null) {
total.append(line);
}