• 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

Can we do this?

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Convert a String say, "Hello" to an integer primitive type?

Thanks.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a homework question? What rules do you want to follow to create this value? (i.e. h == 8, e == 5 etc. or something else?)
[ June 16, 2008: Message edited by: Paul Sturrock ]
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Um.. it's not a homework question. I was doing this,

String s = "20";
int i = Integer.parseInt(s);


And then I tried this,

String s = "hello";
int i = Integer.parseInt(s);
and I got a NumberFormatException. So I was wondering if there is a way I can convert it to int.

Thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes there are a number of ways you can convert it to an int. First thing you need to do is define the rules for this conversion, since "Hello" doesn't have any implicit numeric value. What rules are you going to use?
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Um.. I have no idea. Can you provide me with any link where I can study in detail about this?

Thanks.
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think by 'Rule' Paul means your own rules.

You can convert the string into integer like,

1) String = hello ---> Integer = 8,5,12,12,15 (A to Z == 1-26)
2) String = hello ---> Integer = 104,101,108,108,111 (Ascii value)
3) String = HELLO ---> Integer = 72,69,76,76,79 (Ascii value)
[ June 16, 2008: Message edited by: Vishal Pandya ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With other words, what do you want the result to be?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String.hashCode() will also give you an integer value.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arjun Reddy:
Convert a String say, "Hello" to an integer primitive type?


Java is a very flexible language. you can do all kinds of stuff with it. The difficulty here is that your question doesn't really make a lot of sense. It's kind of like saying "can i convert the string "hello" to a color? or a sound? or a taste?"

YOU have to decide what you mean by converting it to an int. you need to define what is called an 'algorithm' - think of it like a recipe. you would have to define the step by step instructions of what exactly the computer (or a person) should do.

You could convert each letter into some numeric equivalent then sum the total. you could multiply them all together. you could use something more complicated if you wanted a better chance of getting a distinct number for each string.

Then you need to think about case.... is "Hello" treated the same as "HELLO" treated the same as "hello"? What about punctuation?

The difficulty with your post is that the problem is just not well defined. think about these things, and make some decisions. If you then get stuck, come back and ask more questions.
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can't convert "hello" in 10 for example. the that you can is "20" in 20.
as a Arjun Reddy spoke.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try using base 36 arithmetic, which allows you to parse any letter, a=10, z=35. It might work with H as well; I have never tried. But you can't use base 37 arithmetic!
Integer.parseInt("hello", 36);

Might work, might not.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please don't call your threads "Can we do this?" Read this, please.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry about that and thanks for the answers.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
You can try using base 36 arithmetic, which allows you to parse any letter, a=10, z=35. It might work with H as well; I have never tried. But you can't use base 37 arithmetic!
Integer.parseInt("hello", 36);

Might work, might not.



It works!



Even works!

Thanks Ritchie, fun
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic