1

I want to retrieve value from textbox and convert it to integer. I wrote the following code but it throws a NumberFormatException.

String nop = no_of_people.getText().toString();
System.out.println(nop);
int nop1 = Integer.parseInt(nop);
System.out.println(nop1);

The first call to System.out.println prints me the number but converting to integer gives an exception. What am I doing wrong?

10
  • 7
    what does it print ? Can you tell us the exact string Commented May 17, 2012 at 6:29
  • 2
    Always use try/catch when trying to parse a string coming from a textbox. And do basic cleaning, like using the trim() function. Commented May 17, 2012 at 6:31
  • Make sure the trim the string before you convert it to Integer. Commented May 17, 2012 at 6:31
  • 1
    the input given in textbox was 4. First system.out prints 4. Commented May 17, 2012 at 6:32
  • Try wih trim() there could be spaces around Commented May 17, 2012 at 6:32

4 Answers 4

6

Note that the parsing will fail if there are any white spaces in your string. You could either trim the string first by using the .trim method or else, do a replace all using the .replaceAll("\\s+", "").

If you want to avoid such issues, I would recommend you use a Formatted Text Field or a Spinner.

The latter options will guarantee that you have numeric values and should avoid you the need of using try catch blocks.

Sign up to request clarification or add additional context in comments.

Comments

0

Your TextBox may contain number with a white space. Try following edited code. You need to trim the TextBox Value before converting it to Integer. Also make sure that value is not exceeding to integer range.

String nop=(no_of_people.getText().toString().trim());
System.out.println(nop);
int nop1 = Integer.parseInt(nop);
System.out.println(nop1);

Comments

0

Try this:

int nop1 = Integer.parseInt(no_of_people.getText().toString().trim());
System.out.println(nop1);

Comments

0

I would suggest replacing all non-digit characters from String first converting to int:

replaceAll("\\D+", "");

You can use this code:

String nop=(no_of_people.getText().toString().replaceAll("\\D+", ""));
System.out.printf("nop=[%s]%n", nop);
int nop1 = Integer.parseInt(nop);
System.out.printf("nop1=[%d]%n", nop1);

3 Comments

I suggest not to do that. Entering "12.34" or "12,34" (depending on locale to be used for floating point) and getting that converted to the integer 1234 without warning or error may be much worse (depending on the application) than a crash.
@Bananeweizen: OP is trying to convert a String to int not float or double.
I know that. But what matters is,if the user of his application knows that or if that user is protected from making mistakes by just entering a floating point value.

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.