• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting string to int  RSS feed

 
Dominique Ramoney
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Further to my previous problem, I'm trying a solution that requires converting a string value to an int using the ff method;

int val = Integer.parseInt(s);

This however, is throwing a NumberFormatException exception.

My s is a string like "Dominique Ramoney"

Any ideas?
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My s is a string like "Dominique Ramoney"1


Your string above is not a vaild number. So the parseInt method fails. The String must be a valid number.

For e.g.

int val =Integer.parseInt("100");
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dominique Ramoney:
Hi

Further to my previous problem, I'm trying a solution that requires converting a string value to an int using the ff method;

int val = Integer.parseInt(s);

This however, is throwing a NumberFormatException exception.

My s is a string like "Dominique Ramoney"

Any ideas?


I think , you should be use ::

Solution 1. Use String.trim() method for delete blank




Solution 2. Check input before parse (Number only)
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, you can catch the NumberFormatException, so that you know it is not a valid number.

Nick
 
Dominique Ramoney
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx guys

So it appears that since the trim suggestion didn't work, there is no way to represent a string such as "firstname lastname" as an integer?

Oh well, back to the drawing board.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dominique,

Why do you want to convert your name to an integer?

Jules
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dominique Ramoney:
Thanx guys

So it appears that since the trim suggestion didn't work, there is no way to represent a string such as "firstname lastname" as an integer?


Do u mean to convert them into ASCII number? If so, we should approach another way, rather the technique given above...
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why do you want to convert your name to an integer?

I guess he may pass all params into the method for coversion, and only those who can convert to numbers are those necessary data, as we dont have any ideas of whether a series of params are strings or numbers, unless we check it digit by digit before passing to the method.

Nick
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nicholas Cheung:

I guess he may pass all params into the method for coversion, and only those who can convert to numbers are those necessary data, as we dont have any ideas of whether a series of params are strings or numbers, unless we check it digit by digit before passing to the method.

Nick


Nick, don't you think that it's ASCII numbers that the original poster wants... Then surely we need another approach to fulfil the requirement in this case...
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we check it digit by digit before passing to the method.


You can check digit in yor data follow this ::

 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no way to represent a string such as "firstname lastname" as an integer?


You could try getting ASCII value for each character. There is no ASCII value for a String.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dominique Ramoney
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice everyone, I was really just trying to find a kludge but I've got my program working correctly now.

UR all a great help.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dominique Ramoney:
Thanks for the advice everyone, I was really just trying to find a kludge but I've got my program working correctly now.

UR all a great help.


It's good to know that ur program is working properly now... But you didn't clear the thing that we all doubt... Is it like trying to get the ASCII value of each character(as Pradeep mentioned) or finding a valid number in the string(as Nick mentioned)?

Anyway, we are glad to hear that you got your program working properly...
 
Fabrizio Gianneschi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dominique Ramoney:
Thanx guys

So it appears that since the trim suggestion didn't work, there is no way to represent a string such as "firstname lastname" as an integer?

Oh well, back to the drawing board.


I'm not sure if this is the type of solution you're looking for... however, there's a method to generate an unique integer from a String and it's called "hashcode()".

Example:

String s = "abcde";
int i = s.hashcode();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!