Solved

How do you use long in a HashMap?

Posted on 2011-09-15
9
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

623 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