Reputation: 49
I have made research for a couple hours trying to figure out how to convert a String array to a Int array but no luck.
I am making a program where you can encrypt a message by using three rotors. I am able to type a message and get the index number for the first rotor (inner rotor) to encrypt into the third rotor (outer rotor). The problem is the index number is in a string array which I want it to become a int array.
Yes, I have tried
int[] array == Arrays.asList(strings).stream()
.mapToInt(Integer::parseInt).toArray();
or any form of that. I'm not unsure if I have java 8 since it doesn't work, but it gives me an error.
Can someone help me how to convert a String
array to a Int
array?
public void outerRotorEncrypt() {
String[] indexNumberSpilt = indexNumber.split(" ");
System.out.println("indexNumber length: " + indexNumber.length()); //test
System.out.println("indexNumberSpilt length: " + indexNumberSpilt.length); //test
System.out.println("Index Number Spilt: " + indexNumberSpilt[3]); //test
System.out.println("");
System.out.println("");
System.out.println("testing from outerRotorEncrypt");
System.out.println("");
for(int i = 1; i < indexNumberSpilt.length; i++){
secretMessage = secretMessage + defaultOuterRotorCharacterArray[indexNumberSpilt[i]];
}
System.out.println("Secret Message from outerRotorEncrypt: " + secretMessage);
}
Upvotes: 3
Views: 1698
Reputation: 8090
int[]
If you want an int[]
array:
String indexNumber = "1 2 3 4";
String[] indexNumberSpilt = indexNumber.split(" ");
int[] result = Stream.of(indexNumberSpilt).mapToInt(Integer::parseInt).toArray();
System.out.println(Arrays.toString(result));
Integer[]
String indexNumber = "1 2 3 4";
String[] indexNumberSpilt = indexNumber.split(" ");
Integer[] result = Stream.of(indexNumberSpilt).mapToInt(Integer::parseInt).boxed().toArray(Integer[]::new);
System.out.println(Arrays.toString(result));
Both examples will print:
[1, 2, 3, 4]
Upvotes: 0
Reputation: 19622
Small Demo
String[] string = { "0", "1", "11", "0", "0", "1", "11", "0", "0", "1",
"11", "0" };
List<String> temp = Arrays.asList(string);
List<Integer> temp1 = new ArrayList<Integer>();
for (String s : temp) {
temp1.add(Integer.parseInt(s));
}
int[] ret = new int[temp1.size()];
for (int i = 0; i < ret.length; i++) {
ret[i] = temp1.get(i).intValue();
}
System.out.println(Arrays.toString(ret));
}
Upvotes: 0
Reputation: 30809
int[] array == Arrays.asList(strings).stream()
.mapToInt(Integer::parseInt).toArray();
The reason why it's giving an error is because of ==
, changing that to =
(assignment operator) should work, e.g.:
String[] input = new String[]{"1", "2"};
int[] array = Arrays.asList(input).stream()
.mapToInt(Integer::parseInt).toArray();
for(int a : array){
System.out.println(a);
}
Upvotes: 1
Reputation: 361
If you are using Java8 than this is simple way to solve this issue.
List<?> list = Arrays.asList(indexNumber.split(" "));
list = list.stream().mapToInt(n -> Integer.parseInt((String) n)).boxed().collect(Collectors.toList());
In first Line you are taking a generic List Object and convert your array into list and than using stream api same list will be filled with equivalent Integer value.
Upvotes: 2
Reputation: 1879
Have you tried:
int[] array = new int[indexNumberSpilt.lenght()];
for ( int i=0;i<indexNumberSpilt.lenght();i++ ){
array[i] = Integer.valueOf(indexNumberSpilt[i]);
}
Upvotes: 1
Reputation: 19
static int[] parseIntArray(String[] arr) {
return Stream.of(arr).mapToInt(Integer::parseInt).toArray();
}
So take a Stream of the String[]. Use mapToInt to call Integer.parseInt for each element and convert to an int. Then simply call toArray on the resultant IntStream to return the array.
Upvotes: 1