3

How would I convert an array of integers into an integer, but in a special way?

For example, how would I convert { 1, 9, 9, 0 } into 1990?

1
  • 6
    What is the expected behavior if any given array element is outside of the range [0, 9]? Is this homework? What have you tried so far? Commented Aug 24, 2011 at 3:57

7 Answers 7

20

This will work for any size integer array

int nums[] = { 1, 9, 9, 0 };

StringBuilder strNum = new StringBuilder();

for (int num : nums) 
{
     strNum.append(num);
}
int finalInt = Integer.parseInt(strNum.toString());
System.out.println(finalInt);
Sign up to request clarification or add additional context in comments.

5 Comments

That converts to a String, not an integer.
adding an Integer.valueOf() at the bottom should do the trick.
This solution is more correct and general than restricting the numbers to single digits.
@Matt: That's not the point of exceptions. That is, exceptions are not a fix for lack-of-information. You could argue if you knew the correct data, an exception would be appropriate, but CustomerWasVague exception isn't a solution to anything.
You should change strNum to StringBuilder. Concatenating Strings in a loop is a very bad idea.
7
int[] array = {1,9,9,0};
int result = 0;
for(int i = 0; i < array.length; i++) result += Math.pow(10,i) * array[array.length - i - 1];
System.out.println(result);
Output: 1990

2 Comments

Because this is a terrible solution for the reasons I and others have outlined on other posts, except this version is even less readable.
@Stefan: for all you know, this could be exactly the desired behavior (though I do agree that it's less elegant than the deleted one). Nothing in the question enables us to decide between this method and the string concatenation one.
4

It is upsetting to see the unnecessarily complex solutions, involving Math.pow, String etc. Even regex and third party libraries make an appearance!

All you need is a loop and multiplication by 10:

int num = 0;
for (int a : arr) {
  num = 10*num + a;
}

This of course assumes that the elements of arr are in range 0-9; but then the correct behavior outside that case is undefined anyway.

It also assumes that the array's digits can be converted to an integer without overflow; but since the question asks about converting to an integer, overflow behavior is also undefined.


Note that you can handle the "element > 9" case in the similar way to the string concatenation solution by working out how many digits you have to shift num by:

int num = 0;
for (int a : arr) {
  if (a < 0) throw new IllegalArgumentException();
  int i = a;
  do {
    num *= 10;
    i /= 10;
  } while (i != 0);
  num += a;
}

I don't think this is the most appropriate way to handle multiple digits in an element; at the very least the behaviour should be specified. I'm just pointing out that you don't need to resort to strings to handle it.

1 Comment

In my opinion, this is the most simple way which do not waste performance like double cast (from int to string and again to int)
1

Assuming you have digits in Integer[] instead of primitive int[] and have Commons Lang Library you may find following one liner useful

    Integer[] array = {1, 9, 9, 0 };
    System.out.println(Integer.valueOf(StringUtils.join(array)));

Or if the integer is too big to fit in int use BigInteger

    Integer[] piDigits = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2,
            3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1,
            9, 7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9,
            4, 4, 5, 9, 2, 3, 0, 7, 8, 1, 6, 4, 0, 6, 2, 8, 6 };
    System.out.println(new BigInteger(StringUtils.join(piDigits)));

Comments

1

I am not sure in Java, but in pseudo .NET code:

String value = "";
for (int i = 0; i < array.length; i++)
{
    value += array[i]; // Build out the number as a string
}

int someInt = Integer.parseInt(value);

2 Comments

It's like you copy and pasted Shawn's answer directly, but you did it worse by not using the example data.
@Stefan Kendall heh. I was writing my answer while his posted-- but thanks for the comment.
0

Use:

public void turnArrToInt(int x[]){
    for(int i = 0; i < x.length; i++){
        number += x[i]*Math.pow(10,x.length-i-1);
    }

This is how I implemented it. It takes each element, and if it is the first of the array it multiplies it by 10^(length of the array - 1 - theCurrentElement) and it adds all to the result. It works for all sizes. Basically multiplying the 100th by 100, 10th by 10 and so on.

For example: char = {1,2,3} would multiply 1 by 100 2 by 10 3 by 1 and get the sum of all and get 123.

Comments

-1
Arrays.toString(nums).replaceAll("\\D+","");

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.