-2

I want to convert following list into a int array. I have tried to use valueof but I don't know how to use it. I don't want to parse it.

I have a string array here: String[] arr1 = new String[]{"5","5","15","5","10","10", "5", "10", "20", "15"};

Here is the code I tried:

int num = Integer.valueOf(arr1);

I am getting a error message:

error: no suitable method found for valueOf(String[])
    int num = Integer.valueOf(arr1);
                     ^
    method Integer.valueOf(String) is not applicable
      (argument mismatch; String[] cannot be converted to String)
    method Integer.valueOf(int) is not applicable
      (argument mismatch; String[] cannot be converted to int)
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
Plochie
  • 3,944
  • 1
  • 17
  • 33
TheGamer27
  • 25
  • 6

4 Answers4

2

I assume you want to map every value in the arr1 array to an int, and the result should be an int[] (not one int). Stream the array, map the elements to an int, and then invoke toArray. Like,

int[] numArr = Arrays.stream(arr1).mapToInt(Integer::valueOf).toArray();

Note that invoking valueOf in this way is still an example of parsing the String to an int.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Using Streams:

int[] intArray = Stream.of(arr1).mapToInt(Integer::parseInt).toArray();
boot-and-bonnet
  • 731
  • 2
  • 5
  • 16
1

You can iterate through each element of Array and convert it into Int and push it into IntArray.

String[] arr1 = new String[]{"5","5","15","5","10","10", "5", "10", "20", "15"};
int[] intArr = new int[arr1.length];

for (int i = 0; i < arr1.length; i++) {
  intArr[i] = Integer.valueOf(arr1[i]);
}

valueOf internally uses parseInt

public static Integer valueOf(String s) throws NumberFormatException {
    return Integer.valueOf(parseInt(s, 10));
}
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
  • Thank you for your answer, as mentioned I do not want to parse at all. Is there any way I can do it using valueOf? – TheGamer27 Nov 24 '19 at 03:37
  • Yes, you can use `valueOf` instead of `parseInt`. `valueOf` returns an int and not the int arr. That's why you need to push it into int arr either using for each or Stream as other answers has suggested. – Vikalp Patel Nov 24 '19 at 03:40
  • Thank you for the clarification, I am new to programming so I was confused. – TheGamer27 Nov 24 '19 at 03:43
1

Here is how you can do it without using valueOf or parseInt.

      String[] arr1 = new String[] { "5", "5", "15", "5", "10", "10", "5", "10"
      };


      int[] vals = Arrays.stream(arr1).mapToInt(str -> str.chars().reduce(0,
            (val, ch) -> val * 10 + (ch - '0'))).toArray();

      System.out.println(Arrays.toString(vals));

Here are its limitations:

  1. If your strings contain characters other than digits. It will quietly give you the wrong answer.
  2. It does not handle negative values.
  3. It will throw an exception if any of the individual numbers exceed Integer.MAX_VALUE.

My recommendation would be to use Integer.valueOf. That is what it's for.

WJS
  • 36,363
  • 4
  • 24
  • 39