Search code examples
javamatrixbluej

Calling method class twice; once for rows, and then for columns


Calling method getSize() twice for two input answers. It should first go for rows and then for columns. My problem is it prints out..

rows: 6
columns: 4
rows: 6
columns: 4

when it should print out as...

Please enter number of rows: 6

Please enter number of columns: 4

and be a number between 2 and 6.

I know that it runs 4 times because it calls the getSize(args) twice but I am not sure where to go from here.

    public static void main (String [] args)
{
    // Double arrays declared as integers
    int [][] array1;
    int [][] array2;

    // Text to be printed out for user to read
    System.out.print("Welcome to the Matrix Math Calculator\n");
    System.out.print("-------------------------------------\n");
    System.out.print("Each dimension of the matrix must be at least 2,\n");
    System.out.print("and at most 6, in length\n");
    System.out.print("\n");

    // Calls getSize(args) to get the number for rows and then columns
    int rows = getSize(args);
    int columns = getSize(args);

    // Instantiate input for rows and columns into both double arrays
    array1 = new int [rows][columns];
    array2 = new int [rows][columns];

    // For loop to fill in the rows and column values for double array and get random integers
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            array1 [i][j] = (int)(Math.random() * 9 + 1);
            array2 [i][j] = (int)(Math.random() * 9 + 1);
            System.out.printf("%4s",array1[i][j]+ " ");
            System.out.printf("%4s",array2[i][j]+ " ");
        }
        System.out.println(" ");
    }
}

    public static int getSize(String [] args)
{
    // Scanner for keyboard input
    Scanner keyboard = new Scanner(System.in);

    // Declares input as integer
    int input = 0;

    // The string has [2] arguments
    args = new String[2];

    // The string arguments
    args[0] = ("Please enter number of rows: ");
    args[1] = ("Please enter number of columns: ");

    //
    System.out.print(args[0]);
    input = keyboard.nextInt();

    //
    while(input < 2 || input > 7) {
        System.out.print(args[0]);
        input = keyboard.nextInt();
        if (input < 2 || input > 7) 
            System.out.println("The number you entered was not between 2 and 6.");
    }


    //
    System.out.print(args[1]);
    input = keyboard.nextInt();

    //
    while(input < 2 || input > 7) {
        System.out.print(args[1]);
        input = keyboard.nextInt();
        if (input < 2 || input > 7) 
            System.out.println("The number you entered was not between 2 and 6.");
    }

    //
    return input;
}

Solution

  • There is redundancy in your program. In your main() function you are calling getSize() function twice: once for row and once for column. Then in your function getSize() you are taking input for both row and column. So, the last input is being sent to your main function. You should use this getSize() function below,

              public static int getSize(String args){
    
          // Scanner for keyboard input
          Scanner keyboard = new Scanner(System.in);
    
          // Declares input as integer
          int input = 0;
    
          System.out.print(args);
          input = keyboard.nextInt();
    
          while(input < 2 || input > 7) {
              System.out.print(args);
              input = keyboard.nextInt();
              if (input < 2 || input > 7) 
                  System.out.println("The number you entered was not between 2 and 6.");
          }
          return input;
      }
    

    And in main() function use this

    int rows = getSize("Please enter number of rows: ");
    int columns = getSize("Please enter number of columns: ");