• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting string to int

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dominique,

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

Jules
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Dominique Ramoney
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
reply
    Bookmark Topic Watch Topic
  • New Topic