3

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);
}
4
  • 1
    If you're running Java 8, your first attempt should work, please post the error message. Commented Jan 10, 2017 at 9:11
  • How, if at all, does your outerRotorEncrypt method relate to the problem? Commented Jan 10, 2017 at 9:13
  • @MarounMaroun Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at EnigmaClient.introduction(EnigmaClient.java:91) at EnigmaClient.main(EnigmaClient.java:15) Commented Jan 10, 2017 at 9:21
  • It seems like your error does not have anything to do with that method you have shown or with creating the int array, but occurs at an entirely different place. Please show the introduction method and your input. Commented Jan 10, 2017 at 10:02

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

It tells me "1.8.0_111" is that Java 8?
I might have a " " before the string array which couldn't be remove from the split which is why its can't convert into a int..
@AllenTran please provide String value of 'indexNumber' so that I can provide split function properly
0 hence why I start for loop with int i = 1; instead of i = 0;
sorry it's " 5 19 20 20 6 10 6 17 20 15" EDIT: the numbers refer to the index of the rotor.
|
1
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.

1 Comment

OP says this didn't work for him, how's your answer helpful?
1

Have you tried:

int[] array = new int[indexNumberSpilt.lenght()];
for ( int i=0;i<indexNumberSpilt.lenght();i++ ){
   array[i] = Integer.valueOf(indexNumberSpilt[i]);
}

1 Comment

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:592) at java.lang.Integer.valueOf(Integer.java:766) at Enigma.outerRotorEncrypt(Enigma.java:160) at Enigma.innerRotorEncrypt(Enigma.java:131) at Enigma.secretMessage(Enigma.java:85) at Enigma.isRotorValid(Enigma.java:59) at EnigmaClient.introduction(EnigmaClient.java:102) at EnigmaClient.main(EnigmaClient.java:15)
1
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);
}

Comments

0

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));
}

Comments

0

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]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.