1

I am trying to read elements of a 2-d 3x3 matrix using a scanner. The input would look something like this:

3
11 2 4
4 5 6
10 8 -12

I am current geting the error:

Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
scan.next();

List<List<Integer>> array = new ArrayList<>();

for (int i = 0; i < a; i++) {
 
    String number = scan.nextLine();
    String[] arrRowItems1 = number.split(" ");
    List<Integer> list = new ArrayList<>(); 

    for (int j = 0; j < a; j++) {
        int arrItem = Integer.parseInt(arrRowItems1[j]); 
        list.add(arrItem);
    }

    array.add(list);
}

scan.close();

How do I go about doing this problem, so that a the end a 2d 3x3 matrix array is constructed according to user input? Thank You.

2
  • Should the input for one line be 11 2 4 or should this be separate inputs 11, 2 and 4? Commented Aug 4, 2020 at 22:08
  • Hi, Each element in the array is a separate input in a single line. Commented Aug 4, 2020 at 22:10

2 Answers 2

2

Do the following:

Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
scan.nextLine(); // change to nextLine

Your program works just fine the way it was written as long as you make the above change. However, I recommend you split on "\\s+" to allow any number of spaces between the numbers.

Sign up to request clarification or add additional context in comments.

Comments

0

You could read the input line-by-line as a string, check it for validity, parse it and fill it in an int[][]-Matrix.
This way, the program keeps asking for input until it got three valid lines of numbers:

Scanner scan = new Scanner(System.in);
int dim = 3; //dimensions of the matrix
int line = 1; //we start with line 1
int[][] matrix = new int[dim][dim]; //the target matrix

while(line <= dim) {
    System.out.print("Enter values for line " + line + ": "); //ask for line
    String in = scan.nextLine(); //prompt for line
    String[] nums; //declare line numbers array variable
    
    //check input for validity
    if (!in.matches("[\\-\\d\\s]+")
            | (nums = in.trim().split("\\s")).length != dim) {
        System.out.println("Invalid input!");
        continue;
    }
    
    //fill line data into target matrix
    for (int i = 0; i < nums.length; i++) {
        matrix[line - 1][i] = Integer.parseInt(nums[i]);
    }
    
    line++; //next line
}

scan.close(); //close scanner (!)

//test output
for (int i = 0; i < matrix.length; i++) {
    System.out.println(Arrays.toString(matrix[i]));
}

The last for-loop is only for printing the result, just to check if it works:

Enter values for line 1: 11 2 4
Enter values for line 2: 4 5 6
Enter values for line 3: 10 8 -12
[11, 2, 4]
[4, 5, 6]
[10, 8, -12]

BTW by changing the value for dim (dimension) you could even get a 5x5 or whatever matrix!
I hope this helped!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.