Link to home
Start Free TrialLog in
Avatar of gauravflame
gauravflame

asked on

String Array to Int Array

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
        }
  .........
...............
...............

}

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try

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

ASKER

Doesn't work
Error:
']' expected
The line i just posted is fine
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
Doesn't work
 int aInt[j] = Integer.parseInt((String)Elevation[j]);  // Need to FIx
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]);
}

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

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

In what way 'doesn't work'?
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...
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];
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];
>>aInt[j-2] = Integer.parseInt((String)store_value[j]);

Good point
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
        }
  .........
...............
...............

}

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

You're not listening... ;-)
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.
Able to compile without error
So everything fixed now?
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)
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[].
There's probably something other than a number in there
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[]
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do I also have to change from float to int explicitly ?
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
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...  
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
:-)

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

No. You'd need to cast/round
>>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 :)
Oops. Sorry CEHJ - did not see your last post.