Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java convert string to int array

import java.util.Arrays;
public class StringToIntegerArray {
   public static void main(String args[]) {
      String [] str = {"123", "345", "437", "894"};
      int size = str.length;
      int [] arr = new int [size];
      for(int i=0; i<size; i++) {
         arr[i] = Integer.parseInt(str[i]);
      }
      System.out.println(Arrays.toString(arr));
   }
}
Comment

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

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 :: Diamond pattern program in java 
Java :: min priority queue in java 
Java :: arraylist java 
Java :: java print variable type 
Java :: datetimeformatter for java 
Java :: java foreach arraylist 
Java :: hashmap iteration 
Java :: generate 5 digit random string in java 
Java :: How to use Runnable interface to develop multi-threaded Java programs? 
Java :: convert int[] to list java 
Java :: get distance from latitude and longitude code android 
Java :: toCharArray() method java 
Java :: how to check whether a character is alphabet or not in java 
Java :: java string to lower case 
Java :: java send request 
Java :: java File Separator 
Java :: error cannot find symbol intent 
Java :: java get date in utc 
Java :: java for 
Java :: java stream distinct by atribute 
Java :: java random between two strings 
Java :: generate random password in spring boot 
Java :: How to print in console java 
Java :: java responseentity 
Java :: java button size 
Java :: java break 2 for loops 
Java :: rotate a linked list 
Java :: java for each loop 
Java :: java replace a character at end of string 
Java :: primitive data types in java 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =