-1
import java.util.Scanner;

public class Main extends Hashmap{

    public static void main(String[] args) {
        Hashmap hm = new Hashmap();
        int x=0;
        Scanner input = new Scanner(System.in);
        do{
            System.out.print("Enter any integer value between 1 to 12: ");
            x = input.nextInt();
        }while(x<=0 || x>12);
        Scanner sc = new Scanner(System.in);
        //int number;


       do {
           while (!sc.hasNextInt())
           {
            System.out.println("That's not a number!");
            sc.next();
           }
            x = sc.nextInt();
           }while(x>=0);

        String month = hm.getEntry(x);
        System.out.println(month);




}
    }

here I need to restrict user from entering an alphabet.But its not working. pls help...

JoseK
  • 30,355
  • 14
  • 98
  • 129
bhavna raghuvanshi
  • 965
  • 4
  • 14
  • 16
  • 2
    I don't quite have an answer for you, but at a glance... 1. Why are you extending HashMap? It doesn't look like you need to do that. 2. Your variable "hm" has not been populated with any values, so the last two lines where you get "month" won't work. – Ben J Jun 18 '10 at 05:38
  • program is working correctly.just the check is not working. – bhavna raghuvanshi Jun 18 '10 at 05:54

2 Answers2

0

First, have a look at polygenelubricants comprehensive answer to your problem again. I'm pretty sure, he solved it for you in Example 3.

Then, as an alternative design for a user interface: accept any user input, validate the input and if the input is not correct, provide a helpful message and ask the user to enter a value again. This is much, much easier to implement and a user should be happy with this too.

Community
  • 1
  • 1
Andreas Dolk
  • 108,221
  • 16
  • 168
  • 253
0
public static void main(String[] args) {
    HashMap<Integer, String> hm = new HashMap<Integer, String>();
    /**
     * Populate hashmap.. Your logic goes here
     */
    Scanner sc = new Scanner(System.in);
    int x;
    System.out.println("Enter a number between 1 and 12");
    do {
        while (!sc.hasNextInt()) {
            System.out.println("That's not a number! Enter a number between 1 and 12");
            sc.next();
        }
        x = sc.nextInt();
        String month = hm.get(x);
        System.out.println("The corresponding month is " + month);
    } while (x >= 0);

}

I think this is what you are trying to achieve..

chedine
  • 2,364
  • 3
  • 17
  • 24