Results 1 to 4 of 4
Thread: Phonebook HashMap Example
- 01-23-2012, 05:48 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
Phonebook HashMap Example
Hi,
Im having some problems with an example i took from youtube: Lecture 19 | Programming Methodology (Stanford) 44:40
When i run my code the readPhoneNumbers() method does not continue to ask for input. It stops after the first input. It should keep asking for a name and number until i do not enter a name.
Also if enter a small number for the first phone number it will work, if i put in a large number it wont.
I will see some messages like this:
Exception in thread "main" java.util.InputMismatchException: For input string: "95865594539"
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Phonebook.readPhoneNumbers(Phonebook.java:20)
at Phonebook.main(Phonebook.java:9)
Below is my code, it is the same as the example given in the youtube video so i cant see what i am doing wrong.
Any help is greatly appreciated.
Thank you.
Java Code:import java.util.*; public class Phonebook { private static Map<String, Integer> phonebook = new HashMap<String, Integer>(); static Scanner in = new Scanner(System.in); public static void main(String [] args){ readPhoneNumbers(); lookUpNumbers(); displayAllNumbers(); } private static void readPhoneNumbers(){ while(true){ System.out.println("Enter a name: "); String name = in.nextLine(); if(name.equals(""))break; System.out.println("Enter a phone number for " +name +":"); Integer number = in.nextInt(); phonebook.put(name, number); } } private static void lookUpNumbers(){ while(true){ System.out.println("Enter name to look up number: "); String name = in.nextLine(); if(name.equals(""))break; Integer number = phonebook.get(name); if(number == null){ System.out.println(name + " not in phonebook"); }else{ System.out.println(number); } } } private static void displayAllNumbers(){ for(String name: phonebook.keySet()){ Integer number = phonebook.get(name); System.out.println(name + ": " + number); } } }
- 01-23-2012, 06:08 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Phonebook HashMap Example
This looks like you have encountered the joy of a Scanner.
nextInt does not take in the carriage return.
That return sits in the buffer until you get round to the "Enter a name" part.
It then accepts the return as the input for nextLine(), which results in an empty string for name.
Chuck an extra nextLine() call after you've read the number to use up that "spare" return.
As for the number, it's because a long phone number (if treated as a number) is larger than MAX_Integer.
I wouldn't treat phone numbers as numbers. They're strings that happen to contain (mostly) numbers.
- 01-23-2012, 06:09 PM #3
Re: Phonebook HashMap Example
Do you know how large a number an int variable can hold? Is that number larger that the max?Exception in thread "main" java.util.InputMismatchException: For input string: "95865594539"
at java.util.Scanner.nextInt(Unknown Source)
Try using a long to hold the value
- 01-23-2012, 07:09 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
Java Phonebook With Arrays // HELP! //
By K-Scale in forum New To JavaReplies: 9Last Post: 10-04-2011, 03:41 AM -
Help with HashMap
By d0nmin0 in forum Advanced JavaReplies: 8Last Post: 08-15-2011, 01:25 AM -
final HashMap hm=new HashMap();
By sangramkeshari.jena in forum New To JavaReplies: 4Last Post: 07-21-2011, 09:44 PM -
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 07:17 PM -
phonebook update
By nanna in forum New To JavaReplies: 5Last Post: 03-09-2009, 10:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks