Solved

How do you use long in a HashMap?

Posted on 2011-09-15
9
269 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 79
map related example 6 47
Error in @AspectJ Based AOP with Spring 2 13
jsp login check 12 31
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

776 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