Reputation: 13
I have the following issue: The values of each row of my matrix are given, with columns separated by a whitespace - so I enter all row values in a String array, remove the white spaces and parse the numbers into an int array. Now the values of each row look like 1 number "12345", while they should be "1 2 3 4 5".
How can I first separate the digits and then fill my matrix by adding the elements to each row? Thanks! Here is my code:
String n1 = input.nextLine ();
int n = Integer.parseInt(n1); //rows of the matrix
String[] arr = new String [n]; //contains all the rows of the matrix
int [] array = new int [arr.length]; // contains all the elements of the rows of the matrix without whitespace
for (int i = 0; i < arr.length; i++) {
arr [i] = input.nextLine().replaceAll("\\s+","");
array[i] = Integer.parseInt(arr[i]);
}
int matrix [][] = new int [n][arr[0].length()];
Upvotes: 1
Views: 1824
Reputation: 26
It is hard to say but as i understand, you're trying to input a matrix line by line via the Scanner. This could solve your problem.
Scanner scanner = new Scanner(System.in);
//number of rows
int n = Integer.parseInt(scanner.nextLine());
int[][] matrix = new int[n][];
for(int i=0;i<n;i++) {
String line = scanner.nextLine();
String[] numbers = line.split(" ");
matrix[i] = new int[numbers.length];
for(int j=0;j<numbers.length;j++) {
matrix[i][j] = Integer.parseInt(numbers[j]);
}
}
Upvotes: 1
Reputation: 131346
Here you have important issues :
for (int i = 0; i < arr.length; i++) {
arr [i] = input.nextLine().replaceAll("\\s+",""); // loses the separator between the number
array[i] = Integer.parseInt(arr[i]); // makes no sense as you want get all numbers submitted for the current row and no a single one
}
You could do the processing by using much less variables if you populate the matrix at each row submitted.
No tested code but you should get an idea.
String n1 = input.nextLine();
int n = Integer.parseInt(n1); //rows of the matrix
int matrix [][] = null; // init it later : as you would have the two dimensions knowledge
for (int i = 0; i < n; i++) {
String[] numberToken = input.nextLine().split("\\s");
// matrix init : one time
if (matrix == null){ matrix [][] = new int[n][numberToken.length]; }
// array of int to contain numbers of the current row
int[] array = new int[numberToken.length];
// map String to int. Beware exception handling that you should do
for (int j = 0; j < numberToken.length; j++){
array[j] = Integer.parseInt(numberToken[j]);
}
// populate current row of the matrix
matrix[i] = array[j];
}
Upvotes: 1
Reputation: 5446
You should split()
input String by some char (space in your example).
Example how to convert String
to array of String
(using split()
method)
// Example input
String input = "1 2 3 4 5";
// Split elements by space
// So you receive array: {"1", "2", "3", "4", "5"}
String[] numbers = input.split(" ");
for (int position = 0; position < numbers.length; position++) {
// Get element from "position"
System.out.println(numbers[position]);
}
Example how to convert String
to array of int
// Example input
String input = "1 2 3 4 5";
// Split elements by space
// So you receive array: {"1", "2", "3", "4", "5"}
String[] strings = input.split(" ");
// Create new array for "ints" (with same size!)
int[] number = new int[strings.length];
// Convert all of the "Strings" to "ints"
for (int position = 0; position < strings.length; position++) {
number[position] = Integer.parseInt(strings[position]);
}
Upvotes: 1