String Array to Int Array

gauravflame
gauravflame used Ask the Experts™
on
How to convert String[] array to int[] array  ?
I am using the below code ,but it doesn't work for me :
=====================
Info() {
MyTable mytablecal = new MyTable();
Object[] store_value = mytablecal.Row_Column_Calculation();
String[] Name =  (String[])store_value[0];
String[] Elevation =  (String[])store_value[1];
       
         int[] aInt = new int[50];
        for(int j =0 ; j < store_value.length ; j++)
        {
        int aInt[j] = Integer.parseInt(Elevation[j]);  // Need to fix this ..getting error
        }
  .........
...............
...............

}

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Try

int aInt[j] = Integer.parseInt((String)store_value[j]);  // Need to fix this ..getting error

Author

Commented:
Doesn't work
Error:
']' expected
Top Expert 2016

Commented:
The line i just posted is fine
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Top Expert 2016

Commented:
If the name and elevation are in elements 0 and 1, it suggests you'll need

for(int j = 2 ; j < store_value.length ; j++)
{
      int aInt[j] = Integer.parseInt((String)store_value[j]);
}

actually

Author

Commented:
Doesn't work
 int aInt[j] = Integer.parseInt((String)Elevation[j]);  // Need to FIx

Author

Commented:
Your way can also convert Name String[] to int[]

for(int j = 2 ; j < store_value.length ; j++)
{
      int aInt[j] = Integer.parseInt((String)store_value[j]);
}

Author

Commented:
for(int j = 2 ; j < store_value.length ; j++)
{
      int aInt[j] = Integer.parseInt((String)store_value[j]);
}

=============Above mention code doesn't work
Top Expert 2016

Commented:
>>=============Above mention code doesn't work

In what way 'doesn't work'?

Commented:
What does does not work mean?
- Does not compile? Hard to believe.
- Throws an error - are you sure that the array has only numbers in it?
- Something else - no crystyal ball so please specify...

Commented:
Oops. Saw it. It should be
for(int j = 2 ; j < store_value.length ; j++)
{
      aInt[j] = Integer.parseInt((String)store_value[j]);
}

The type is already declared -- you just need to put the value

And I would change int[] aInt = new int[50]; to
int[] aInt = new int[store_value.length];

Commented:
And maybe it should actually be
for(int j = 2 ; j < store_value.length ; j++)
{
      aInt[j-2] = Integer.parseInt((String)store_value[j]);
}This way the Int array wil be filled from the beginning... and not starting from the third element while from the initial one will be read the elements starting at #2.

Then the int[] aInt = new int[store_value.length]; may be int[] aInt = new int[store_value.length-2];
Top Expert 2016

Commented:
>>aInt[j-2] = Integer.parseInt((String)store_value[j]);

Good point

Author

Commented:
It throwing an error on IDE.

Under my MyTable Class , I have a method "Object[] Row_Column_Calculation()"
which will return value
return new Object[]{State_Name,Elevation};
---------------------
Under my another class B I am using method Info() .I am using these return values .
I don't want to Change the String[] Name  array , I just like to change the String[] Elevation into int[] array

Below is the code for Info :

Info() {
MyTable mytablecal = new MyTable();
Object[] store_value = mytablecal.Row_Column_Calculation();
String[] Name =  (String[])store_value[0];
String[] Elevation =  (String[])store_value[1];
       
         int[] aInt = new int[50];
        for(int j =0 ; j < store_value.length ; j++)
        {
        int aInt[j] = Integer.parseInt(Elevation[j]);  // Need to fix this ..getting error
        }
  .........
...............
...............

}

Top Expert 2016

Commented:
>>int aInt[j] = Integer.parseInt(Elevation[j]);  // Need to fix this ..getting error

You're not listening... ;-)

Commented:
Just apply the same thing....

         int[] aInt = new int[Elevation.length];
        for(int j =0 ; j < Elevation.length ; j++) // you had the wrong array here
        {
        aInt[j] = Integer.parseInt(Elevation[j]);  // Need to fix this ..getting error
        }

If it still fails, post the errors.

Author

Commented:
Able to compile without error

Commented:
So everything fixed now?

Author

Commented:
I am getting error like while running the code

  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
        at java.lang.Integer.parseInt(Integer.java:456)
        at java.lang.Integer.parseInt(Integer.java:497)

Commented:
You are not reading what we tell you....
Some of the values in your String[] are not numbers - make sure that all of them are before trying to make the int[].
Top Expert 2016

Commented:
There's probably something other than a number in there

Author

Commented:
Is there a way I can return 2 values from the method one will be String[] and another will be int[]
So Class B will not have to convert the String[]  to int[]
Top Expert 2016
Commented:
return Object[]

return new Object[] { stringArray, intArray };

Author

Commented:
Yes , Some values are float ,I am sure about it.
values like 34.5,89,1,2,34.9,56.2 ,9.2,8.3 are there in String[] Elevation
Commented:
Then how do you expect to make a int array from them?!?!?!?

You may use double[] and Double.parseDouble instead for example

Author

Commented:
Do I also have to change from float to int explicitly ?

Author

Commented:
My fault ,
My requirement is to make String[] Elevation to int[] Array .
After changing to Double , we will again change Double[] to int[] ..I am right

Commented:
ok... how you plan to put 14.2 in int?
With some type of rounding?

If so something like this may be an answer:
aInt[j] =(int) (Double.parseDouble(Elevation[j]));
with no double[]

But this will just remove the part after . 14.2 will be saved as 14, 14.9 as 14 as well. If you need rounding you will need to play with Math.round() or something like this...  

Author

Commented:
I was looking to make 14.2 to 14 , something like that .
Right Now everything is fine with my code. It accept also Double value .
I was thinking to pass Int[] values instead of Double[] (you figure it out) .
I was in the impression that any floating-point values in String will change to int[] , at that time rounding the values are in my mind .
Thanks you both of you guys
Top Expert 2016

Commented:
:-)

>>any floating-point values in String will change to int[]

No. You'd need to cast/round

Commented:
>>I was in the impression that any floating-point values in String will change to int[]

Nope. Because Integer.parseInt can only produce an int and MUST receive valid input...  A simple cast as shown above should get you on track :)

Commented:
Oops. Sorry CEHJ - did not see your last post.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial