I'm trying to create a matrix that will be defined from user input, I've been trying many methods, the user should input line by line with spaces in between numbers, the program should stop inputting when it gets a -1 as input so the input should be like:
3 4 5 6
1 2 3 4
6 7 8 9
-1
and this is exactly how my created matrix should look like, please help. The thing is initially I dont know the size of the matrix, the user decides on the size based on what they input. code should be in java.
Here is some code I used, I used it to find the size of the matrix so I can actually make the matrix, it stores inputs in an array though so I dont know now how I can export them from the array to the 2d array.
import java.util.*;
public class Program {
public static void main(String[] args) {
// write your code here
Scanner in = new Scanner(System.in);
ArrayList<String>strArray = new ArrayList<>(); //stores-inputs
String input = in.nextLine();
input.replaceAll(" ", "");
while(!input.equals("-1")){
strArray.add(input);
input = in.nextLine();
input.replaceAll(" ", "");
}
int row = strArray.size();
String col = input.replaceAll(" ", "");
int noCol = col.length();
int[][] matrix = new int[row][noCol];
}}
CodePudding user response:
Here is one way
- it takes in a line of separated by one or more spaces
- splits the line and converts to an
int
array - then ensures capacity of the existing target and assigns the array.
- it stops when the line equals
end
. This allows for input of -1
Scanner input = new Scanner(System.in);
int[][] finalArray = new int[0][];
String line;
while (!(line = input.nextLine()).contains("end")) {
int[] arr = Arrays.stream(line.split("\\s ")).mapToInt(Integer::parseInt).toArray();
finalArray = Arrays.copyOf(finalArray,finalArray.length 1);
finalArray[finalArray.length-1] = arr;
}
for (int[] a : finalArray) {
System.out.println(Arrays.toString(a));
}
I would recommend either prompting for the row and col count and then allocate the array or use ArrayLists
which grow dynamically.
Note: Anything other than an int
value to place in the array will throw an exception
CodePudding user response:
You could use a List<List<Integer>>
?
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<List<Integer>> mat = new ArrayList<List<Integer>>();
System.out.print("Row Values (-1 to quit): ");
String input = in.nextLine().trim();
while (!input.equals("-1")) {
List<Integer> values = new ArrayList<Integer>();
for(String strValue : input.split("\\s ")) {
try {
values.add(Integer.parseInt(strValue));
} catch (NumberFormatException e) {}
}
if (values.size() > 0) {
mat.add(values);
}
System.out.print("Row Values (-1 to quit): ");
input = in.nextLine().trim();
}
for(List<Integer> row : mat) {
System.out.println(row);
}
}
Sample run:
Row Values (-1 to quit): 3 4 5 6
Row Values (-1 to quit): 1 2 3 4
Row Values (-1 to quit): 6 7 8 9
Row Values (-1 to quit): -1
[3, 4, 5, 6]
[1, 2, 3, 4]
[6, 7, 8, 9]
CodePudding user response:
If I'm getting your question correctly, you can fill the matrix with this option:
Scanner in = new Scanner(System.in);
ArrayList<Integer> nrsArray = new ArrayList<>();
String input = in.nextLine();
int row = 0;
while (!input.equals("-1")) {
char[] chars = input.replaceAll(" ", "").toCharArray();
for (char c : chars) {
nrsArray.add(Character.getNumericValue(c));
}
row ;
input = in.nextLine();
}
int col = nrsArray.size() / row;
int[][] matrix = new int[row][col];
for (int y = 0; y < row; y ) {
for (int x = 0; x < col; x ) {
matrix[y][x] = nrsArray.get(x y * col);
}
}
System.out.println(Arrays.deepToString(matrix));