-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
6
  • How would I use valueOf? Commented Nov 24, 2019 at 3:37
  • If you want to convert those string representations of numbers to ints, then you do want to parse them. Commented Nov 24, 2019 at 3:37
  • Thank you for your answer.I know parsing is probably easier but I am told to not use it. Commented Nov 24, 2019 at 3:39
  • Thank you for the clarification, I am new to programming so I was confused. Commented Nov 24, 2019 at 3:43
  • 3
    Possible duplicate of Converting a String array into an int Array in java Commented Nov 24, 2019 at 4:36

4 Answers 4

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.

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

2 Comments

How would I use valueOf?
Thank you for the clarification, I am new to programming so I was confused.
1

Using Streams:

int[] intArray = Stream.of(arr1).mapToInt(Integer::parseInt).toArray();

3 Comments

Thank you for your answer, as mentioned I do not want to parse at all. Is there any other way to do it.
valueOf calls parseInt internally so not sure you have a choice
Thank you for the clarification, I am new to programming so I was confused.
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));
}

3 Comments

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?
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.
Thank you for the clarification, I am new to programming so I was confused.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.