Reputation: 83
I need to create code that takes user input to create the size of the 2d array (rows and columns must be less than 6), then fill it up with values that the user gives (between -10 and 10). My loop for taking the values isn't working at all and I'm not sure why. Here's the code
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Implement scanner
Scanner input = new Scanner(System.in);
// create loop for accepting matrix input
// first accept row size
System.out.println("Please enter the number of rows in your matrix. Must be 5 or less.");
int row = input.nextInt();
while (row > 5 || row < 1) {
System.out.println("Sorry. Your number is not the correct size. "
+ "Please enter the number of rows in your matrix. Must be between 5 and 1.");
row = input.nextInt();
}
// next accept column size
System.out.println("Please enter the number of columns in your matrix. Must be 5 or less.");
int column = input.nextInt();
while (column > 5 || column < 1) {
System.out.println("Sorry. Your number is not the correct size. "
+ "Please enter the number of columns in your matrix. Must be between 5 and 1.");
column = input.nextInt();
}
// declare array with row and columns the user gave
int[][] userArray = new int[row][column];
// create loop for accepting values within the matrix
// first loop for row values
for (int i = 0; i < userArray.length; i++) {
System.out.println("Please enter numbers between -10 and 10 for your matrix rows");
int rowValues = input.nextInt();
while (rowValues > 10 || column < -10) {
System.out.println(
"Sorry. Your number is not the correct size. " + "Please enter a number between 10 and -10.");
rowValues = input.nextInt();
}
// second embedded loop for column values
for (int j = 0; j < userArray[i].length; j++) {
System.out.println("Please enter numbers between -10 and 10 for your matrix columns");
int columnValues = input.nextInt();
while (columnValues > 10 || column < -10) {
System.out.println("Sorry. Your number is not the correct size. "
+ "Please enter a number between 10 and -10.");
columnValues = input.nextInt();
}
}
}
printMatrix(userArray);
}
Upvotes: 0
Views: 11593
Reputation: 18255
public class Matrix {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of rows.");
int rows = getValueFromConsole(scan, 1, 5);
System.out.println("Enter number of columns.");
int cols = getValueFromConsole(scan, 1, 5);
System.out.println("Enter matrix data.");
int[][] matrix = createMatrix(scan, rows, cols, -99, 99);
printMatrix(matrix);
}
private static int[][] createMatrix(Scanner scan, int rows, int cols, int min, int max) {
int[][] matrix = new int[rows][cols];
for (int row = 0; row < rows; row++)
for (int col = 0; col < cols; col++)
matrix[row][col] = getValueFromConsole(scan, min, max);
return matrix;
}
private static int getValueFromConsole(Scanner scan, int min, int max) {
while (true) {
System.out.format("Number [%d:%d]: ", min, max);
int val = scan.nextInt();
if (val >= min && val <= max)
return val;
System.out.println("Sorry. Your number is not correct. Try again.");
}
}
private static void printMatrix(int[][] matrix) {
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++)
System.out.format("%4d", matrix[row][col]);
System.out.println();
}
}
}
Output:
Enter number of rows.
Number [1:5]: 3
Enter number of columns.
Number [1:5]: 3
Enter matrix data.
Number [-99:99]: 10
Number [-99:99]: -11
Number [-99:99]: 12
Number [-99:99]: -13
Number [-99:99]: 14
Number [-99:99]: -15
Number [-99:99]: 16
Number [-99:99]: -17
Number [-99:99]: 18
10 -11 12
-13 14 -15
16 -17 18
Upvotes: -1
Reputation: 6006
You need a nested loop, to read values at row and then column level. Something like below:
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Implement scanner
Scanner input = new Scanner(System.in);
// create loop for accepting matrix input
// first accept row size
System.out.println("Please enter the number of rows in your matrix. Must be 5 or less.");
int row = input.nextInt();
while (row > 5 || row < 1) {
System.out.println("Sorry. Your number is not the correct size. "
+ "Please enter the number of rows in your matrix. Must be between 5 and 1.");
row = input.nextInt();
}
// next accept column size
System.out.println("Please enter the number of columns in your matrix. Must be 5 or less.");
int column = input.nextInt();
while (column > 5 || column < 1) {
System.out.println("Sorry. Your number is not the correct size. "
+ "Please enter the number of columns in your matrix. Must be between 5 and 1.");
column = input.nextInt();
}
// declare array with row and columns the user gave
int[][] userArray = new int[row][column];
for(int i=0;i< row ; i++){
for(int j=0; j< column; j++) {
System.out.print("Please enter the value for array["+i+"]["+j+"] (between -10 and 10):");
int val = input.nextInt();
if(val>10 || val< -10) {
System.out.println("Invalid value.");
continue;
}
userArray[i][j] = val;
}
}
printMatrix(userArray, row, column);
}
public static void printMatrix(int[][] array, int row, int column) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
Upvotes: 0
Reputation: 1
Couple of things here. First, your for-loop has two separate loops for the rows and columns. I assume what you're going for is prompting the user for each individual cell of the 2D array, which would require two nested loops. Second, at no point does your code example assign the user input to a variable, so I presume your output is just a bunch of 0 values?
You're probably looking for something more like this:
for (int i = 0; i < userArray.length; i++)
for(int j = 0; j < userArray[i].length; j++)
{
System.out.println("Please enter numbers between -10 and 10 for your matrix");
int valueIn = input.nextInt();
while (valueIn > 10 || valueIn < -10)
{
System.out.println("Sorry. Your number is not the correct size. " + "Please enter a number between 10 and -10.");
valueIn = input.nextInt();
}
userArray[i][j]=valueIn;
}
Upvotes: 0