0

How to split string containing numbers into int array i.e

String s="12345";
int i[]=new int[5]; 
i[0]=1; 
i[1]=2;
i[2]=3; 
i[3]=4; 
i[4]=5;

i have tried it by

String s="12345";
int i[]=new int[5];
int tem = Integer.parseInt(s);
for(int t=0;t<5;t++){
i[4-t]=tem%10;
tem=tem/10;
}

it is giving right answer in above case but in case of String s="73167176531330624919225119674426574742355349194934" it fails so any other way or how to use split method in above case

1
  • 1
    You can go through the characters (charAt, length of String) and -'0' from them. That would give you the numbers which you can store in an int[]. Commented Jan 18, 2015 at 10:39

4 Answers 4

6

You can use Character.getNumericValue(char)

String str = "12345";
int[] nums = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
    nums[i] = Character.getNumericValue(str.charAt(i));
}
Sign up to request clarification or add additional context in comments.

2 Comments

your solution give correct answer but cant we use split method here??
Split method splits your string by an expression. You string contains only numbers so you do not have a regular expression for splitting it. For example if your string would be like "1 2 3 4" you can use method split(" ") and convert each new formed string into an integer.
2

That's because your number wont fit in the integer range nor in long range. Also to note, Your code wont be that efficient due to division and modulas operator as well. Instead you could always use charAt api of String and convert individual characters to a number as give below:

String s = "73167176531330624919225119674426574742355349194934";
int[] numbers = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
   numbers[i] = s.charAt(i) - '0';
}
System.out.println(Arrays.toString(numbers));
Output:
[7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4, 9, 1, 9, 2, 2, 5, 1, 1, 9, 6, 7, 4, 4, 2, 6, 5, 7, 4, 7, 4, 2, 3, 5, 5, 3, 4, 9, 1, 9, 4, 9, 3, 4]

1 Comment

char represents an ASCII/unicode digit (like '1'), and you subtract the smallest possible ASCII/unicode digit from it (e.g. '0'), then you'll be left with the digit's corresponding value i.e. int 1.
1

The culprit is this line of code:

int tem = Integer.parseInt(s);

When you enter a large number is string which is outside the range of what an int can accomodate, the overflow happens, and thus all of a sudden you are working on a different number than what was in your string.

I would suggest you iterate over each character of the string, and then convert each character to integer:

for (char ch: s.toCharArray()) {
    // convert ch to integer, and add to the array.
    intArray[i] = (int)(ch - '0');
    // of course keep incrementing `i`
}

2 Comments

your solution give correct answer but cant we use split method here?? of yes then what is its syntex
@piyank: You can, but there is no need of. split() method would generally be applicable, where you want to split your string based on complex splitter, which involves regex. Here since you just want to read each character, directly getting character array seems appropriate.
0

You can simple use Character wrapper class and get the numeric value and add it to array of in.

        String sample = "12345";

        int[] result = new int[sample.length()];

        for(int i=0;i<result.length;i++)
        {
            result[i] = Character.getNumericValue(sample.charAt(i));
        }

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.