Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

String to Int Array

For the values of the array given by separated with space " " you can try this cool one liner Java 8 & onwards suppported streams based solution:

Scanner scan = new Scanner(System.in);
double[] arr = Arrays.stream(scan.nextLine()
                                  .trim()
                                  .split(" "))
                                  .filter(x -> !x.equals(""))
                                  .mapToDouble(Double::parseDouble)
                                  .toArray();
                                  
For int array you can try:

 Scanner scan = new Scanner(System.in);
 int[] arr = Arrays.stream(scan.nextLine()
                                  .trim()
                                  .split(" "))
                                  .filter(x -> !x.equals(""))
                                  .mapToInt(Integer::parseInt)
                                  .toArray();
                                  
With filter() method you can also avoid more than one spaces in between the inputs.
Source by www.sitepoint.com #
 
PREVIOUS NEXT
Tagged: #String #Int #Array
ADD COMMENT
Topic
Name
4+6 =