Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to convert string array to int in java

int[] numbers = Arrays.stream(line.split(",")).mapToInt(Integer::parseInt).toArray();
Comment

string to int array

String s = "1234";
int[] intArray = new int[s.length()];

for (int i = 0; i < s.length(); i++) {
	intArray[i] = Character.digit(s.charAt(i), 10);
}

Comment

Converting String Array to an Integer Array

int[] arr = Stream.of(new String[]{"1", "2", "3"})
                  .mapToInt(Integer::parseInt).toArray();
System.out.println(Arrays.toString(arr));
Comment

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.
Comment

convert string to int array

char c = '2';
int asInt = c - '0';
//asInt = 2 
Comment

PREVIOUS NEXT
Code Example
Java :: iterate hashmap java 
Java :: make edittext not editable android 
Java :: nested list java 
Java :: java convert float to double 
Java :: find elements in selenium 
Java :: longest common subsequence of two strings 
Java :: java iterate hashmap 
Java :: java constructor overloading 
Java :: arraylist add new element to end 
Java :: hollow pyramid star pattern in java 
Java :: check stack empty java 
Java :: java method reference 
Java :: deserialize date java 
Java :: center textview programmatically android 
Java :: how to add string to array in java 
Java :: convert string into time in java 
Java :: how to change color of drawable in android 
Java :: regex pattern for bank cards validation 
Java :: java android development find element by id 
Java :: java map merge example 
Java :: uses or overrides a deprecated API. 
Java :: how to stop activity from another activity 
Java :: the main of java 
Java :: what is var in java 
Java :: java round up 
Java :: hibernate in spring boot 
Java :: java character 
Java :: java get class name of object 
Java :: window in java swing 
Java :: activityViewModels 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =