0

I am creating a small Linear Algebra library with matrix addition, scalar multiplication, getting the transpose, etc., with a constructor that takes as parameters the dimensions of the desired matrix.

The idea is to be able to return a matrix of the given size with either randomized elements or with user-specified elements when creating the object, and I am using Scanner to let the user choose between the two, and also to input the specified elements. But I want to be able to automate a response to the Scanner with e.g. "yes" to get a matrix with randomized elements or just a matrix with all 1's for testing.

I have tried using System.out.println("yes") hoping that it would answer the Scanner but obviously this doesn't work.

 public static void main(String[] args){
        Matrix matrix1 = new Matrix(4,4);
        System.out.println("yes");

This is the constructor

Matrix(int rowSize, int colSize){
        Scanner scan = new Scanner(System.in);
        System.out.println("Do you want randomized elements?");
        String myChoice = scan.nextLine();
        if(Pattern.matches("(?i)[ye]+s*", myChoice)){
            NewMat(rowSize, colSize, 1);
        }
        else{
            NewMat(rowSize, colSize, 0);
        }
        System.out.println(GetMat());
    }

I want to be able to readily create a matrix with e.g. all elements as 1 or instantly get a randomized matrix without having to answer the Scanner each time. Is there a way to give a response to the Scanner directly using the main method? That is, is it possible to do something like creating the matrix and using System.out.println("yes") to get randomized elements (I know this doesn't work, but this is an example of what kind of solution I am looking for), and thus avoiding having to write in the terminal?

  • 1
    So, you could just move the question you have in the constructor, to main? and send another parameter to the constuctor suggesting whether the numbers should be randomized or not – Diaco May 14 '19 at 10:25

2 Answers2

3

use a third parameter input which has three possible values. say no_data, yes, no

Matrix(int rowSize, int colSize, String input) {
  Scanner scan;
  String myChoice;
  if (!input.equals("no_data") {
      scan = new Scanner(System.in);
      System.out.println("Do you want randomized elements?");
      myChoice = scan.nextLine();
    } else {
      myChoice = input;
    }
    if (Pattern.matches("(?i)[ye]+s*", myChoice)) {
      NewMat(rowSize, colSize, 1);
    } else {
      NewMat(rowSize, colSize, 0);
    }
    System.out.println(GetMat());
  }
}

then you can call the constructor

Matrix matrix1 = new Matrix(4,4,"yes");
Roshana Pitigala
  • 7,058
  • 8
  • 38
  • 66
Madhusudhan Aradya
  • 482
  • 1
  • 6
  • 14
  • 1
    If there's only there possible values always, you can use `Boolean` instead. `null`, `true`, `false`. Makes much easier when using with `if`s – Roshana Pitigala May 14 '19 at 10:46
1

Maybe this can work for you:

System.setIn(new FileInputStream(new File("mytestscenario.txt""));

Source: JUnit: How to simulate System.in testing?

Conffusion
  • 4,092
  • 2
  • 13
  • 19