-3

I'm trying to store name and address of persons in the form of 2d array, but when I run my code it accepts less values only. For example if I give the array 2 rows and 2 columns, it accepts only 3 values.

I've tried searching on other forums, couldn't get the proper answer. I also changed the dimension values but it gives wrong result only.

import java.util.*;
class findme{
    public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        System.out.print("enter the number of person: ");
        int per=scan.nextInt();
        System.out.print("enter the number of address: ");
        int addr=scan.nextInt();
        String addrs[][]=new String[per][addr];
        for(int i=0;i<per;i++){
            for(int j=0;j<addr;j++){
                addrs[i][j]=scan.nextLine();
            }
        }
    }
}
Matthew
  • 1,779
  • 3
  • 15
  • 23
  • This is probably due to your use of the `Scanner` API. – Tim Biegeleisen Jun 04 '19 at 14:37
  • 1
    Please use the java naming convention of classes to start with uppercase. FindMe or Findme in this case – paulo.bing Jun 04 '19 at 14:39
  • 1
    Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Johnny Mopp Jun 04 '19 at 14:42

3 Answers3

2

You read 4 values but one is an empty line from when you press enter for int addr=scan.nextInt();

A quick fix is to read that empty line

import java.util.*;
class findme{
    public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        System.out.print("enter the number of person: ");
        int per=scan.nextInt();
        System.out.print("enter the number of address: ");
        int addr=scan.nextInt();
--->        scan.nextLine();
        String addrs[][]=new String[per][addr];
        for(int i=0;i<per;i++){
            for(int j=0;j<addr;j++){
                addrs[i][j]=scan.nextLine();
            }
        }
    }
}

Edit:

Or you can use scanner.skip Skip newline character while reading from Scanner class

Butiri Dan
  • 1,704
  • 5
  • 10
  • 18
0

In addition to the other answer here is how your code should look like:

    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the number of people: ");
    int iPeople = scanner.nextInt();

    System.out.print("Enter the number of address: ");
    int iAdresses =scanner.nextInt();

    scanner.nextLine();

    String data[][] = new String[iPeople][iAdresses];
    for(int i=0; i < iPeople; i++)
    {
        for(int j=0; j < iAdresses; j++)
        {
            System.out.printf("Enter %d address for person %d:%n", j + 1, i + 1);
            data[i][j] = scanner.nextLine();
        }
    }

And try to follow these conventions:

  • Use proper Java naming conventions
  • Make your code more readable by providing appropriate empty lines between lines you feel are too cluttered or belong to different groups based on what operation are they trying to accomplish.
Matthew
  • 1,779
  • 3
  • 15
  • 23
0

If you want to understand why, the behavior of nextLine is explain here : Java Scanner doesn't wait for user input

You can also replace nextLine by next to avoid this.