+2 votes
1,273 views
How can i convert String datatype to Integer in jsp ?

Any examples ?
asked Mar 10, 2016 in Computer Science - IT by Dharani
edited Mar 10, 2016 by slow_learner | 1,273 views
always post programs that you have tried to convert the same...

2 Answers

0 votes

You can use method :

Integer.parseInt(String)

Consider you have integer as num and string is name

int num= 0;

//Always Check if string is not null or empty , in either case it will show error
 if(name !=null && !name.isEmpty())
//trim() removes space in string
int num = Integer.parseInt(name.trim());
answered Mar 10, 2016 by slow_learner
Hi article is realy nice, i will suggest alternative method for parsing using java8, wecan use these methods in parsing like,
1. Set.Stream()
2. Stream.map()
3. Collectorts.toset() it is the best way to convert string to int.
i hope this may be use full.
@kavya Can u please explain with example code?
0 votes

Let’s look at three different ways to convert a string into a number.

parseInt() The parseInt() method converts a string into an integer (a whole number).It accepts two arguments. The first argument is the string to convert. The second argument is called the radix. This is the base number used in mathematical systems. For our use, it should always be 10.

parseFloat() The parseFloat() method converts a string into a point number (a number with decimal points). You can even pass in strings with random text in them.

Number() # The Number() method converts a string to a number.

 Sometimes it’s an integer. Other times it’s a point number. And if you pass in a string with random text in it, you’ll get NaN, an acronym for “Not a Number.”

 As a result of this inconsistency, it’s a less safe choice than parseInt() and parseFloat(). If you know the format of the number you’d like, use those instead. If you want the string to fail with NaN if it has other characters in it, Number() may actually be a better choice.


answered Feb 25 by Likhit_N