Solved

int representation of a string

Posted on 2006-07-09
4
245 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
ID: 17070430
String sid = "123";
int id = Integer.parseInt(sid);

unique according to what exactly?
0
 

Author Comment

by:mb2297
ID: 17070449
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
ID: 17070461
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
ID: 17070484
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Which is the latest version of Apache Tomcatl 3 51
eclipse formatting 6 74
ArrayIndexOutOfBoundException 9 68
simple java question 3 44
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

863 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

26 Experts available now in Live!

Get 1:1 Help Now