converting string to int in java

  • Thread starter Thread starter GeX
  • Start date Start date

GeX

GeX

Soldato
Joined
17 Dec 2002
Posts
7,016
Location
Manchester
hi. can anyone help me out with this?

i want to convert a string to an int, specifics of the int is not important. It does not have to be the ascii equivilant of it. I'm not sure the best way to approaching this.

Am working in Java, would it make sense to declare the string as an array of char and then loop through, pulling each char out and adding its ascii equivilant int to a running total?

How would i go about coding that. I'm a tad rusty!

edit: the outcome i want from this, is a number generated based on the string. that's all.
 
Last edited:
Integer.parseInt("10"); for example.

If for some odd reason you want to do it like you said:

Code:
        String s = "test";
        
        int i = 0;
        int sum = 0;
        while (i < s.length())
        {
            sum =+ s.charAt(i++);
        }
 
Last edited:
Or do you want to make a something like this?

Code:
int count = 0;
String s = "abcdef";

CharacterIterator iter = new StringCharacterIterator(s);

for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
	count += Character.getNumericValue(c);
}

/edit beaten
 
thanks guys, the reason i suggested doing as i did is because that is how i can work through it in my head.

Integer.parseInt("10"); <-- much simpler, but i'm not sure how to use that. Can you put it in some context?
 
It just turns a numerical string into an integer.

For example

Code:
        String x = "10";       
        int z = Integer.parseInt(x);
        System.out.println(z);

Sounds like its not what you want anyhow.
 
no, that wouldn't work. i won't have ints in the string, it'll be actual characters.

basically i want to convert abcdefgh to an int
 
So what exactly are you tring to do? convert "a" into 1 "b" into 2 etc. Is there any relationship between the String and the number?
 
i want to generate a unique number based on the string, maybe convert each character to its ascii value and then add them together. hence my slightly odd way of looking at it when i first posted!

the hashCode of any object contains an int i think.

Code:
String str = "hello";
int i = str.hashCode();
System.out.println(i);

thats something i didn't think of, cheers. looks like that;ll do what i want, ta!
 
Last edited:
How about this then:

Code:
    public int stringToInt(String str)
    {
        int total = 0;
        char[] chars = str.toCharArray();
        for (char c : chars) {
            total += (int)c;
        }
        return total;
    }

It takes a String and gets an array of characters from the String. It then iterates over each character and converts it to an integer (via a type cast) which gets added to a total for the current String, the total is then returned.

Here is s sample of calling the above method and the results:

Code:
System.out.println(stringToInt("a"));
System.out.println(stringToInt("b"));
System.out.println(stringToInt("c"));
System.out.println(stringToInt("abc"));

Output:

97
98
99
294

This seems to do what you want and is basically the idea you mentioned in the OP.
 
that's even better, thanks for that. it 'fits' better because it's how i was thinking it would work. ta. :)
 
No problem, easy once I understood that you wanted to generate a number based on the string, your original post makes sense now. They are ASCII values too (or ASCII values added together if it's more than a single character string) if you press Alt + 97 (on your number pad) you will get 'a'.
 
Last edited:
that's even better, thanks for that. it 'fits' better because it's how i was thinking it would work. ta. :)
This method will give the same number for "abc" as it will "cba" or for that matter "$d]A".

Do you want them to be unique?
 
it would be better, yeah.

could use both methods, using the hashcode to convert the int to a unique number.
 
Last edited:
It depends on the application, you can override the hashcode() method and write your own hashing function (you could do a MD5 or SHA1 hash in it as Fourstar suggested) if you require an unique value. If you know the range of the input data then writing an effective hashing function that prevents hash collisions shouldn't be that difficult. Without knowing the specific scenario it's hard to say what works best.
 
Back
Top Bottom