aspose file tools*
The moose likes Beginning Java and the fly likes Can we do this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can we do this?" Watch "Can we do this?" New topic
Author

Can we do this?

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Convert a String say, "Hello" to an integer primitive type?

Thanks.


Be Humble... Be Nice.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Nov 10, 2007
Posts: 627
Um.. I have no idea. Can you provide me with any link where I can study in detail about this?

Thanks.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
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 ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3556
    
  15
String.hashCode() will also give you an integer value.


Joanne
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
you can't convert "hello" in 10 for example. the that you can is "20" in 20.
as a Arjun Reddy spoke.


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
And please don't call your threads "Can we do this?" Read this, please.
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
I am sorry about that and thanks for the answers.
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
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


Java hobbyist.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we do this?