Getting Matrix Input - Sam36502/SimpleTUI GitHub Wiki
Setup
All input requires the input scanner to be open. For more info see Input Setup.
Integer Matrix
To receive an integer matrix, or two-dimensional array of integers,
you can use the MatrixInput.getIntMatrix(width, height)
. This will prompt the user
row by row for the contents of a matrix. It also displays a representation of the
matrix which contains the content already entered and the currently selected square.
The user is asked to enter their value for each cell and once they have filled the
matrix, they are asked to confirm its contents. If they don't confirm, they can fill
in the matrix again with different data. Otherwise, the matrix is returned. Also note,
the screen is "cleared" after every value that is entered. For more information on
screen clearing Output.
Example:
MatrixInput.getIntMatrix(2, 2);
[#][ ]
[ ][ ]
Enter the value for the selected cell.
--> 5
[5][#]
[ ][ ]
Enter the value for the selected cell.
--> 11
[ 5][11]
[##][ ]
Enter the value for the selected cell.
--> 420
[ 5][ 11]
[420][###]
Enter the value for the selected cell.
--> 27
[ 5][ 11]
[420][ 27]
Is this correct?
(y/n) --> y
// Array {{5, 11}, {420, 27}} is returned
Double Matrix
Getting a matrix of doubles is almost identical to getting integers, except you can also enter decimal numbers.
Example:
MatrixInput.getDoubleMatrix(2, 2);
[#][ ]
[ ][ ]
Enter the value for the selected cell.
--> 5.7237
[5.7237][######]
[ ][ ]
Enter the value for the selected cell.
--> 3.14159
[ 5.7237][3.14159]
[#######][ ]
Enter the value for the selected cell.
--> 4.20
[ 5.7237][3.14159]
[ 4.20][#######]
Enter the value for the selected cell.
--> 1.41
[ 5.7237][3.14159]
[ 4.20][ 1.41]
Is this correct?
(y/n) --> y
// Array {{5.7237, 3.14159}, {4.20, 1.41}} is returned
String Matrix
Getting a matrix of strings is also the same as with integer and double matrices, but you can enter strings instead of numbers.
Example:
MatrixInput.getStringMatrix(2, 2);
[#][ ]
[ ][ ]
Enter the value for the selected cell.
--> Isn't
[Isn't][#####]
[ ][ ]
Enter the value for the selected cell.
--> pretty
[ Isn't][pretty]
[######][ ]
Enter the value for the selected cell.
--> print
[ Isn't][pretty]
[ print][######]
Enter the value for the selected cell.
--> wonderful?
[ Isn't][ pretty]
[ print][wonderful?]
Is this correct?
(y/n) --> y
// Array {{"Isn't", "pretty"}, {"print", "wonderful?"}} is returned