Solved

int representation of a string

Posted on 2006-07-09
4
244 Views
Last Modified: 2010-03-31
Hello experts,

I need to take a unique id which is a string and convert it into an integer. How can I do that and ensure the integer is also unique?

Cheers,
Matt.
0
Comment
Question by:mb2297
  • 2
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
String sid = "123";
int id = Integer.parseInt(sid);

unique according to what exactly?
0
 

Author Comment

by:mb2297
Comment Utility
Sorry, I should have said the String is alphanumeric - parseInt will throw a NumberFormatException in that case.

I'm basically trying to override Object's hashCode() function.

Each of my objects is a row in a table, so they have a row identifier (an int) and a table identifier (a String). I want to concatenate the two together to form an id which can uniquely id any row in any table.

However, hashCode() requires that you return an int - so I need to parse that unique alphanumeric id into a unique integer id.

Hope that makes sense. I should have been more clear, sorry.

Thanks,
Matt.
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
Comment Utility
return value from hashCode does not need to be unique.
you could just return the hashCode() of the concatenated string you have
0
 

Author Comment

by:mb2297
Comment Utility
Hello again objects.

In the interest of HashMap efficiency, I would like the codes to be unique, and returning the code of the concatentated string works just fine.

Many thanks!
Matt.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now