0

I have a .txt in my website that contains a number (in this case 3) and I use this code to check whether this number is greater than or less than another number, but the code gives me this error:

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

This is my code:

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");
        }
3
  • 1
    Your String casa doesn't just contain the number 3, it also may contain multiple new line \n characters, which cannot be parsed into numbers. Commented Mar 3, 2015 at 15:45
  • String casa = EntityUtils.toString(ht). And remove the while-loop, without appending \n Commented Mar 3, 2015 at 15:45
  • I forget to remove the while because it's only one line. It's my mistake I'm sorry Commented Mar 3, 2015 at 15:52

2 Answers 2

1

I agree with Kon, your variable "casa" is containing another characters.

Try using the trim() method:

 if (Integer.parseInt(casa.trim())>4){
...
...
...

but now i see that you are appending the "\n", in total variable, is this "new line" necessary?:

 while ((line = r.readLine()) != null) {
            total.append(line);
        }
Sign up to request clarification or add additional context in comments.

Comments

0

Try this to remove all whitespace that might be surrounding your number:

if (Integer.parseInt(casa.trim()) > 4){
    // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.