Solved

int representation of a string

Posted on 2006-07-09
4
249 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 50
check java version using powershell 13 178
Is there a simpler dropbox system? 10 34
hashmap order 17 37
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

831 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