Reputation: 101
Suppose I have a string "1 23 40 187 298". This string only contains integers and spaces. How can I convert this string to an integer array, which is [1,23,40,187,298]. this is how I tried
public static void main(String[] args) {
String numbers = "12 1 890 65";
String temp = new String();
int[] ary = new int[4];
int j=0;
for (int i=0;i<numbers.length();i++)
{
if (numbers.charAt(i)!=' ')
temp+=numbers.charAt(i);
if (numbers.charAt(i)==' '){
ary[j]=Integer.parseInt(temp);
j++;
}
}
}
but it doesn't work, please offer some help. Thank you!
Upvotes: 10
Views: 34820
Reputation: 124215
You are forgetting about
temp
to empty string after you parse it to create place for new digitsthat at the end of your string will be no space, so
if (numbers.charAt(i) == ' ') {
ary[j] = Integer.parseInt(temp);
j++;
}
will not be invoked, which means you need invoke
ary[j] = Integer.parseInt(temp);
once again after your loop
But simpler way would be just using split(" ")
to create temporary array of tokens and then parse each token to int
like
String numbers = "12 1 890 65";
String[] tokens = numbers.split(" ");
int[] ary = new int[tokens.length];
int i = 0;
for (String token : tokens){
ary[i++] = Integer.parseInt(token);
}
which can also be shortened with streams added in Java 8:
String numbers = "12 1 890 65";
int[] array = Stream.of(numbers.split(" "))
.mapToInt(token -> Integer.parseInt(token))
.toArray();
Other approach could be using Scanner
and its nextInt()
method to return all integers from your input. With assumption that you already know the size of needed array you can simply use
String numbers = "12 1 890 65";
int[] ary = new int[4];
int i = 0;
Scanner sc = new Scanner(numbers);
while(sc.hasNextInt()){
ary[i++] = sc.nextInt();
}
Upvotes: 15
Reputation: 1
I met similar question in android development. I want to convert a long string into two array -String array xtokens and int array ytokens.
String result = "201 5 202 8 203 53 204 8";
String[] tokens = result.split(" ");
String[] xtokens = new String[tokens.length/2 + 1];
int[] ytokens = new int[tokens.length/2 + 1];
for(int i = 0, xplace = 0, yplace = 0; i<tokens.length; i++){
String temptoken = new String(tokens[i]);
if(i % 2 == 0){
xtokens[xplace++] = temptoken;
}else {
ytokens[yplace++] = Integer.parseInt(temptoken);
}
}
You can first convert this string into an string array separated by space, then convert it to int array.
Upvotes: 0
Reputation: 3199
For java 8+ you can use this way:
final Integer[] ints = Arrays.stream(numbers.split(" "))
.map(Integer::parseInt)
.toArray(Integer[]::new);
or, if you need primitive ints, you can use this:
final int[] ints = Arrays.stream(numbers.split(" "))
.mapToInt(Integer::parseInt)
.toArray();
Upvotes: 7
Reputation: 109
Try this out,
public static void main(String[] args) {
String numbers = "12 1 890 65";
String[] parts = numbers.split(" ");
int[] ary = new int[4];
int element1 = Integer.parseInt(parts[0]);
int element2 = Integer.parseInt(parts[1]);
int element3 = Integer.parseInt(parts[2]);
int element4 = Integer.parseInt(parts[3]);
ary[0] = element1;
ary[1] = element2;
ary[2] = element3;
ary[3] = element4;
for(int i=0; i<4; i++){
System.out.println(ary[i]);
}
}
Upvotes: 0
Reputation: 8348
Reset the tmp String to "" after you parse the integer unless you wish to continue to append all the numbers of the String together. There are also alternatives as well - for instance splitting the String into an array on the space characeter, and then parsing the numbers individually
Upvotes: 0