1

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()];
1
  • Your description doesn’t match your code. Commented Mar 8, 2019 at 15:37

3 Answers 3

1

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]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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];
}

5 Comments

Thanks for the ideas! The part with int matrix [][] = null; was very helpful, I didn't know it was possible :)
You are still out. Boy, that is one long vacation. I hope you are doing fine ... Unrelated: if you have a minute or two, please have a look at stackoverflow.com/a/55650516/1531124 and let me know what you think about the two answers given on that question.
@GhostCat Hello you. Yes fine and you how are you ? Indeed it was a very long vacation. And I will probably be much less present on SO next months too. So take my email ebundy at gmail.com if you want to ask me anything. About it, is your request always valid ?
Glad to hear from you. You got mail. I hope that worked. I received feedback on that answer already, but feel free to comment. I always try to understand when other people claim they know better.
I get it and I will answer on it. By the way thanks for the other comment :)
1

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]);
        }
    }

1 Comment

Yes, it's exactly what I was trying to do, but I'm still struggling with parsing elements entered dynamically. Your code helped a lot and I understand things better now. Thank you! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.