Solved

How do you use long in a HashMap?

Posted on 2011-09-15
9
277 Views
Last Modified: 2012-05-12
I am trying to use long instead of integer to create a phoneBook. It keeps saying its an integer and its to large.

import java.util.HashSet;
import java.util.HashMap;
/**
 * Write a description of class MapTester here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class PhoneBook
{
private HashMap<String, Long> phoneBook = new HashMap<String, Long> ();
private HashSet<String, Long> phoneBook = new HashSet<String, Long>();

public PhoneBook()
{
phoneBook.put("Homer Jay Simpson", 53193924587);
phoneBook.put("Peter Griffin", 53154321945);
phoneBook.put("Apu Nahasapeemapetilon", 53142344418);
}


/**
 * Method that takes two parameters the name and telephone number and then attempts to add information into the phonebook
 */
public void phoneBook(String name, long number)

{
if(phoneBook.put(name, number);
}


public String lookupNumber(String name)

{
return phoneBook.get(name);
}
}
0
Comment
Question by:ryanbecker24
9 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36544718
You shoul put letter L in the end of the long number
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36544722
phoneBook.put("Homer Jay Simpson", 53193924587L);
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36544725
Otherwise it does not know that it is long
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:ryanbecker24
ID: 36544748
Ok thanks
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36544814
how about this

phoneBook.put("Apu Nahasapeemapetilon", new Long(53142344418));
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36544952
with autoboxing you don't need to create new Long(...), as soon as compiler understands
that you are providing long number and for that you need to add "L" or "l"
In general if you write numeber without decimal point Java will treat it as integer, untill you add "L" or "l" to it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36545175
A phone number is not an integral value - it's a String, so you need Map<String, String>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36545193
For a more durable design, you need something other than someone's name as the key in your Map, or the phone book will break if you have duplicate names
0
 

Expert Comment

by:osmanad
ID: 36551786
It is best to initialize Long class with given integer.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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