Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could read the entire input line from scanner, then split the line by <code>,</code> then you have a <code>String[]</code>, parse each number into <code>int[]</code> with index one to one matching...(assuming valid input and no <code>NumberFormatExceptions</code>) like</p> <pre><code>String line = scanner.nextLine(); String[] numberStrs = line.split(","); int[] numbers = new int[numberStrs.length]; for(int i = 0;i &lt; numberStrs.length;i++) { // Note that this is assuming valid input // If you want to check then add a try/catch // and another index for the numbers if to continue adding the others (see below) numbers[i] = Integer.parseInt(numberStrs[i]); } </code></pre> <hr> <p>As <a href="https://stackoverflow.com/a/37093052">YoYo's answer</a> suggests, the above can be achieved more concisely in Java 8:</p> <pre><code>int[] numbers = Arrays.stream(line.split(",")).mapToInt(Integer::parseInt).toArray(); </code></pre> <hr> <p><strong>To handle invalid input</strong></p> <p>You will need to consider what you want need to do in this case, do you want to know that there was bad input at that element or just skip it.</p> <p>If you don't need to know about invalid input but just want to continue parsing the array you could do the following:</p> <pre><code>int index = 0; for(int i = 0;i &lt; numberStrs.length;i++) { try { numbers[index] = Integer.parseInt(numberStrs[i]); index++; } catch (NumberFormatException nfe) { //Do nothing or you could print error if you want } } // Now there will be a number of 'invalid' elements // at the end which will need to be trimmed numbers = Arrays.copyOf(numbers, index); </code></pre> <p>The reason we should trim the resulting array is that the invalid elements at the end of the <code>int[]</code> will be represented by a <code>0</code>, these need to be removed in order to differentiate between a valid input value of <code>0</code>.</p> <p>Results in</p> <blockquote> <p>Input: "2,5,6,bad,10"<br> Output: [2,3,6,10]</p> </blockquote> <p>If you need to know about invalid input later you could do the following:</p> <pre><code>Integer[] numbers = new Integer[numberStrs.length]; for(int i = 0;i &lt; numberStrs.length;i++) { try { numbers[i] = Integer.parseInt(numberStrs[i]); } catch (NumberFormatException nfe) { numbers[i] = null; } } </code></pre> <p>In this case bad input (not a valid integer) the element will be null.</p> <p>Results in</p> <blockquote> <p>Input: "2,5,6,bad,10"<br> Output: [2,3,6,null,10]</p> </blockquote> <hr> <p>You could potentially improve performance by not catching the exception (<a href="https://stackoverflow.com/questions/237159/whats-the-best-way-to-check-to-see-if-a-string-represents-an-integer-in-java">see this question for more on this</a>) and use a different method to check for valid integers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload