JAVA-How to split string to int array based on spaces

preneond

I have multiple line input where each line is line of matrix. I save this line as string and after that i want to split this string based on spaces, but due to better readability the number of spaces between number is not defined. So when i want to parse to int afterwards, it throws an error because, on some places there are more than one space. Is there any solution how i could fix that problem? Thanks

Here is my code, how i tried to solved that

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
int[][] matrix=new int[n][n];
String[] temp;
for (int row = 0; row < n; row++) {
    line =br.readLine();
    temp = line.split("\\s+");
    for(int i=0;i<n;i++){
    matrix[i][row]=Integer.parseInt(temp[i]);
}

here is examplary input

10 10  0 10  5
 5 20 10  7 12
 1  2  3  5  9
10 15 20 35  2
 2 15  5 15  2
ericbn

The issue is when you have spaces in the beginning of the String:

"10 10  0 10  5".split("\\s+"); // ["10", "10", "0", "10", "5"]
" 5 20 10  7 12".split("\\s+"); // ["", "5", "20", "10", "7", "12"]

So then you get an extra empty String. Adding an extra trim() should help:

line.trim().split("\\s+");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to split string to an array of strings without multiple spaces

From Dev

How to split string to an array of strings without multiple spaces

From Dev

Split a string in java based on white spaces escaping those spaces in double quotes and single quotes and that which are preceded by \

From Dev

How do I split a string, without spaces, into multiple words in java?

From Dev

How to split a string on spaces in Clean?

From Dev

How to split a java string based on newline character

From Dev

Split string array to int array

From Dev

How to split file with spaces in JAVA

From Dev

How to store a split string as an array in Java?

From Dev

Java regex to split string with variable spaces and delimiter

From Dev

How to split a string of array of arrays of integers into List<int[]>?

From Dev

How to split a numeric string and then store it in an int array in c?

From Dev

How to split a string array based on dynamic entries of values?

From Dev

How to split a string and store into array based on pattern in shell script

From Dev

How to split string based on pre-defined values from Array

From Dev

How to split a string with two continuous spaces

From Dev

How to split a string on line spaces/breaks in javascript

From Dev

How to split string at comma but keep white spaces?

From Dev

Regex based string split in Java

From Dev

How to split string array?

From Dev

How to split a String into two arrays based on a delimiter in Java?

From Dev

How to split a string based on given any condition in java?

From Dev

Java convert string to string array by spaces

From Dev

String to int array [][] - JAVA

From Dev

Java. How can I split a string with multiple spaces on every nth space?

From Dev

How to invoke substring(int, int) on the array type String[] in Java

From Java

How to split a string in Java

From Dev

How to split the string in java by \?

From Dev

Split long string at the spaces

Related Related

  1. 1

    How to split string to an array of strings without multiple spaces

  2. 2

    How to split string to an array of strings without multiple spaces

  3. 3

    Split a string in java based on white spaces escaping those spaces in double quotes and single quotes and that which are preceded by \

  4. 4

    How do I split a string, without spaces, into multiple words in java?

  5. 5

    How to split a string on spaces in Clean?

  6. 6

    How to split a java string based on newline character

  7. 7

    Split string array to int array

  8. 8

    How to split file with spaces in JAVA

  9. 9

    How to store a split string as an array in Java?

  10. 10

    Java regex to split string with variable spaces and delimiter

  11. 11

    How to split a string of array of arrays of integers into List<int[]>?

  12. 12

    How to split a numeric string and then store it in an int array in c?

  13. 13

    How to split a string array based on dynamic entries of values?

  14. 14

    How to split a string and store into array based on pattern in shell script

  15. 15

    How to split string based on pre-defined values from Array

  16. 16

    How to split a string with two continuous spaces

  17. 17

    How to split a string on line spaces/breaks in javascript

  18. 18

    How to split string at comma but keep white spaces?

  19. 19

    Regex based string split in Java

  20. 20

    How to split string array?

  21. 21

    How to split a String into two arrays based on a delimiter in Java?

  22. 22

    How to split a string based on given any condition in java?

  23. 23

    Java convert string to string array by spaces

  24. 24

    String to int array [][] - JAVA

  25. 25

    Java. How can I split a string with multiple spaces on every nth space?

  26. 26

    How to invoke substring(int, int) on the array type String[] in Java

  27. 27

    How to split a string in Java

  28. 28

    How to split the string in java by \?

  29. 29

    Split long string at the spaces

HotTag

Archive