2

I have a small task that allows the user to enter the regions and their neighbors of any country.

I did everything and I just have a small problem which is when I run my code and the program asks the user to enter the number of regions, if the user enters 13 or and number greater than 10, the system will consider that number is like two inputs and it will not allow the user to enter anything for the second question and it will prompt him with the third question immediately. Why?

I think the problem with the Scanner class in the following command:

Scanner kb = new Scanner(System.in);
System.out.print("Please enter the number of regions: ");
        int REGION_COUNT = kb.nextInt();
        region = new CountryRegion[REGION_COUNT]; 
        String[] neighbours;
        for (int r = 0; r < region.length; r++) {
            System.out.print("Please enter the name of region #" + (r + 1) + ": ");
            String regionName  = kb.nextLine();
            System.out.print("How many neighbors for region #" + (r + 1) + ": ");
            if (kb.hasNextInt()) {
                int size = kb.nextInt();
                neighbours = new String[size];
                for (int n = 0; n < size; n++) {
                    System.out.print("Please enter the neighbour #" + (n + 1) + ": ");
                    neighbours [n] = kb.nextLine();
                }
                region [r] = new CountryRegion(regionName, neighbours);

            } 
            else
                System.exit(0); 
        }
        for (int i = 0; i < REGION_COUNT; i++) {
            System.out.print(region[i].getRegionName() +": ");
            for (int k = 0; k < region[i].getRegionAjesint().length; k++) {
                System.out.print(region[i].getRegionAjesint()[k] +", ");
            }
            System.out.println();
        }
        mapColor = new MapColor(region);

Any help, please?

Mike G
  • 4,022
  • 9
  • 41
  • 63
  • 1
    Post more of your code. With just that we can not see what you are really doing. – unholysampler Dec 30 '10 at 20:08
  • I doubt highly its with the scanner.nextInt() method – Woot4Moo Dec 30 '10 at 20:13
  • Seems odd... I can only suggest you to debug... – bluish Dec 30 '10 at 21:22
  • Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Brydenr Sep 23 '16 at 21:27

3 Answers3

3

Ok, Very simple your problem is that you are using the nextInt() method of the Scanner class and then using the nextLine() method both of these use the same buffer and here is what's happening.

When you enter the number your asking (let say 10) in the key board your actually entering

10 and the enter key (new line character (\n))

The nextInt() method from the Scanner class will read the 10 and just the 10 that meaning that the new line character (\n) is still in the keyboard buffer and next in your code you have a nextLine() which will read everything up to a new line (\n), which you already have in the buffer!!!

So the way this is all working is that the nextLine() method considers the new line character (\n) left in the buffer as it's input and there for continues to the next iteration of the loop.

The solution to your problem is to clear the buffer of the new line character (\n) you can achieve this by calling a nextLine() method before the actual one in your code like so:

...
int REGION_COUNT = kb.nextInt();
region = new CountryRegion[REGION_COUNT]; 
String[] neighbours;
kb.nextLine(); //CLEAR THE KEYBOARD BUFFER
for (int r = 0; r < region.length; r++) {
     System.out.print("Please enter the name of region #" + (r + 1) + ": ");
     String regionName  = kb.nextLine();
...

This way the nextLine() called extracts the new line character from the buffer, clearing it, and since it doesn't store it it gets discarded leaving you with a new line character free buffer ready to receive full input from the user in your nextLine() method.

Hope this helps.

Wemilianius
  • 170
  • 1
  • 5
1

Sounds like each key press is an input.

Will
  • 5,605
  • 4
  • 28
  • 47
0

You are using nextLine() to get the String but I think you should be using next().

jzd
  • 23,019
  • 7
  • 51
  • 76