This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
Joined: Nov 10, 2007
Um.. I have no idea. Can you provide me with any link where I can study in detail about this?
With other words, what do you want the result to be?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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);
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);