Results 1 to 4 of 4
  1. #1
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default 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);
    		}
    	}
    	
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default 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.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Phonebook HashMap Example

    Exception in thread "main" java.util.InputMismatchException: For input string: "95865594539"
    at java.util.Scanner.nextInt(Unknown Source)
    Do you know how large a number an int variable can hold? Is that number larger that the max?
    Try using a long to hold the value

  4. #4
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: Phonebook HashMap Example

    putting in in.newLine() and changing the variable type to Long has sorted out the errors.
    Thanks for the help.

Similar Threads

  1. Java Phonebook With Arrays // HELP! //
    By K-Scale in forum New To Java
    Replies: 9
    Last Post: 10-04-2011, 04:41 AM
  2. Help with HashMap
    By d0nmin0 in forum Advanced Java
    Replies: 8
    Last Post: 08-15-2011, 02:25 AM
  3. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 10:44 PM
  4. Replies: 7
    Last Post: 12-08-2009, 08:17 PM
  5. phonebook update
    By nanna in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •