I need help with trying to convert a String of number to a Integer Array. I will then be adding the to Arrays together like any addition problem, but I am having trouble with just getting the string in to the arrays without it outputting jargon.

public static int[] makeArray(String b){

            int[]arr = new int[b.length()];
            arr[0] = 0;
                for (int i = 1; i < b.length(); i++){
                     arr[i] = Character.digit(b.charAt(i),10);
		}
            return arr;

          }

this is my current output.

Enter First Number: 2135432
Enter Second Number: 1234
[I@48b8f82d + [I@48b8f82d = [I@67ad77a7

Recommended Answers

you need a method that takes an array and produces a String, so effectively the reverse of your original method.

This no doubt is what your teacher wants you to create!

And oh, using all static methods in a single class like that is considered Very Bad (tm) by professionals.

Jump to Post

All 13 Replies

Re: Convert a String to an Int[] array 80 80

I am not sure what you intend to do. Why do u need to create an array of Integers from one String. As far as I think one String will correspond to one Integer. Why do you need an array. For converting one string to an Integer value you can use Integer.parseInt() method.

What your output suggest is you are adding the two arrays address, instead of adding the content in that array

Re: Convert a String to an Int[] array 80 80

I am not sure what you intend to do. Why do u need to create an array of Integers from one String. As far as I think one String will correspond to one Integer. Why do you need an array. For converting one string to an Integer value you can use Integer.parseInt() method.

What your output suggest is you are adding the two arrays address, instead of adding the content in that array

Yes, I am trying to add two arrays, and I cannot use the parseInt method. I need to do it this way because it is what I was told I had to do for a assignment in my CS class. The array is used for extremely large uneven Integers.

Re: Convert a String to an Int[] array 80 80

seems to work just fine. The only thing you've not yet figured out is how to print the array in such a way you actually see what's in it.
That shouldn't be too hard :)

And then there's learning to write proper object oriented code of course.

Re: Convert a String to an Int[] array 80 80

seems to work just fine. The only thing you've not yet figured out is how to print the array in such a way you actually see what's in it.
That shouldn't be too hard :)

And then there's learning to write proper object oriented code of course.

That is why I need the help.

Say I input a string x = "1234567890";
How would I get the array of this string to print.

And, the way I write the code is exactly how I am being taught to, if I deviate from that then I lose credit, IE. fail.

Thank you for this much, I will try to see if I can figure it out though.

Re: Convert a String to an Int[] array 80 80

Now, I have this. And my Arrays do Print know. But now I cant seem to make the result of Add print.

public static int[] Add (int[]a, int[]b, int[]c){

            if (a.length < b.length) {
                int[] tmp = a;
                a = b;
                b = tmp;
            }
            c = new int[a.length];
            int diff = 0;

            for (int i = a.length-1; i >=0; i--)
                int sum = a[i] + b[i]+ diff;
                diff = 0;
                    if (sum > 9 && i > 0){
                        sum -= 10;
                        diff = 1;
                    }
                c[i] = sum;
            }
            return c;
        }


        public static void prtString(int[] b){

            int i = 0;

            for(i = 0;i < b.length;i++){         
                System.out.print(b[i]);
            }
        }

OUTPUT:

Enter First Number: 1234
Enter Second Number: 1234

1234 + 1234 = (THIS IS WHAT I NEED)


Thanks in advance.

Re: Convert a String to an Int[] array 80 80

you need a method that takes an array and produces a String, so effectively the reverse of your original method.

This no doubt is what your teacher wants you to create!

And oh, using all static methods in a single class like that is considered Very Bad (tm) by professionals.
Were such code to arrive on my desk for review it would be instantly rejected.

commented: He help me see the answer in a clear way. +1
Re: Convert a String to an Int[] array 80 80
public static int[] Add (int[]a, int[]b, int[]c){

totally you misunderstood your assignment...

review the assignment and comeback....

Re: Convert a String to an Int[] array 80 80

I have it working now at adding two arrays with the same number of elements, but when I try to add two arrays with unequal number of elements it gives me the ArrayOutOfBounds error.

public void Add (int[]a,int[]b){
          
            int sum = 0;
            flip(a);
            flip(b);
            
            if (a.length < b.length) {
                int[] tmp = a;
                a = b;
                b = tmp;
            }
            
            int[] c = new int[a.length];
            int diff = 0;

            for (int i = a.length-1; i >=0; i--){

                sum = a[i] + b[i]+ diff;
                diff = 0;
                    if (sum > 9 && i > 0){
                        sum -= 10;
                        diff = 1;
                    }

                c[i] = sum;
                
            }
                flip(c);
                print(c);

        }

        public static void flip(int[] b) {
            int left  = 0;         
            int right = b.length-1;
  
            while (left < right) {
    
                int temp = b[left]; 
                b[left]  = b[right]; 
                b[right] = temp;
     
                left++;
                right--;
            }
        }
Re: Convert a String to an Int[] array 80 80

Because 'a' and 'b' don't have the same length:

for (int i = a.length-1; i >=0; i--){
   sum = a[i] + b[i]+ diff;
}

At the above code you check only if the 'i' will be able to be applied at the 'a' array. But you don't check the length of the 'b' array.

Re: Convert a String to an Int[] array 80 80

Because 'a' and 'b' don't have the same length:

for (int i = a.length-1; i >=0; i--){
   sum = a[i] + b[i]+ diff;
}

At the above code you check only if the 'i' will be able to be applied at the 'a' array. But you don't check the length of the 'b' array.

I modified the Add method so it would flip the array, and add correctly. Only now, it is not returning the remaining numbers. Hopefully you will be able to understand after reviewing the code.

public void Add (int[]a,int[]b){

            flip(a);
            flip(b);
            int sum = 0;
            int[] temp;
            
            if(a.length < b.length){
                temp = b;
                b = a;
                a = temp;
                
            }

            int[] c = new int[b.length];
            int diff = 0;

            for (int i = b.length-1; i >=0; i--){

                sum = a[i] + b[i]+ diff;
                diff = 0;
                    if (sum > 9 && i > 0){
                        sum -= 10;
                        diff = 1;
                    }

                c[i] = sum;
                
            }
                flip(c);
                print(c);

        }

Output:
Enter First Number: 67890
Enter Second Number: 2

67890 + 2 = \*missing the rest of the numbers "6789"*\ 2

Re: Convert a String to an Int[] array 80 80

I modified the Add method so it would flip the array, and add correctly. Only now, it is not returning the remaining numbers. Hopefully you will be able to understand after reviewing the code.

public void Add (int[]a,int[]b){

            flip(a);
            flip(b);
            int sum = 0;
            int[] temp;
            
            if(a.length < b.length){
                temp = b;
                b = a;
                a = temp;
                
            }

            int[] c = new int[b.length];
            int diff = 0;

            for (int i = b.length-1; i >=0; i--){

                sum = a[i] + b[i]+ diff;
                diff = 0;
                    if (sum > 9 && i > 0){
                        sum -= 10;
                        diff = 1;
                    }

                c[i] = sum;
                
            }
                flip(c);
                print(c);

        }

Output:
Enter First Number: 67890
Enter Second Number: 2

67890 + 2 = \*missing the rest of the numbers "6789"*\ 2

The logic you used two errors. First of all you are not summing up the remaining digits of a, so you are not getting it in output. Secondly when you will add no like 4567+42 in give result like 109(leaving the remaining numbers).

For solution, I guess After flipping array b. Create a new array(say d) of length larger array(a). Equate starting indexes with array b, and fill up the remaining with zeros. And instead of running the loop for smaller array length, run it for the larger array length(i.e. a). And add array, new array d and the diff. This will solve both the problem, probably...

Re: Convert a String to an Int[] array 80 80

The logic you used two errors. First of all you are not summing up the remaining digits of a, so you are not getting it in output. Secondly when you will add no like 4567+42 in give result like 109(leaving the remaining numbers).

For solution, I guess After flipping array b. Create a new array(say d) of length larger array(a). Equate starting indexes with array b, and fill up the remaining with zeros. And instead of running the loop for smaller array length, run it for the larger array length(i.e. a). And add array, new array d and the diff. This will solve both the problem, probably...

I dont really understand how another array would helps since c[] is already holding a.length. can you explain a little more.

public void Add (int[]a,int[]b){

            
            int sum = 0;
            Swap(a,b);// makes b[] the smaller array

            

            flip(a); // filps array
            flip(b); // 123 becomes 321

            int[] c = new int[a.length];
            int diff = 0;

            for (int i = b.length-1; i >=0; i--){

                sum = a[i] + b[i]+ diff;
                diff = 0;
                    if (sum > 9 && i > 0){
                        sum -= 10;
                        diff = 1;
                    }

                c[i] = sum;
                
            }
                flip(c);
                printArray(c);

        }

Output:
Enter First Number: 123456789
Enter Second Number: 123456
123456789 + 123456 = 0005791416
(doesn't bring down remaining integers)

Enter First Number: 123456789
Enter Second Number: 123456789
123456789 + 123456789 = 2468035719
(works fine, if equal)

Enter First Number: 123456
Enter Second Number: 123456789
123456 + 123456789 =
(ArrayIndexOutOfBoundsException)

Re: Convert a String to an Int[] array 80 80

My Answer on convertion was solve so I will move to a different thread for my Addition problem. Thanks everyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.