Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

How do you use long in a HashMap?

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
ryanbecker24
Asked:
ryanbecker24
1 Solution
 
for_yanCommented:
You shoul put letter L in the end of the long number
0
 
for_yanCommented:
phoneBook.put("Homer Jay Simpson", 53193924587L);
0
 
for_yanCommented:
Otherwise it does not know that it is long
0
Independent Software Vendors: 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!

 
ryanbecker24Author Commented:
Ok thanks
0
 
Gurvinder Pal SinghCommented:
how about this

phoneBook.put("Apu Nahasapeemapetilon", new Long(53142344418));
0
 
for_yanCommented:
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
 
CEHJCommented:
A phone number is not an integral value - it's a String, so you need Map<String, String>
0
 
CEHJCommented:
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
 
osmanadCommented:
It is best to initialize Long class with given integer.
0

Featured Post

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now