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_Calc ulation();
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
}
.........
...............
...............
}
I am using the below code ,but it doesn't work for me :
=====================
Info() {
MyTable mytablecal = new MyTable();
Object[] store_value = mytablecal.Row_Column_Calc
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
}
.........
...............
...............
}
ASKER
Doesn't work
Error:
']' expected
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)s tore_value [j]);
}
actually
for(int j = 2 ; j < store_value.length ; j++)
{
int aInt[j] = Integer.parseInt((String)s
}
actually
ASKER
Doesn't work
int aInt[j] = Integer.parseInt((String)E levation[j ]); // Need to FIx
int aInt[j] = Integer.parseInt((String)E
ASKER
Your way can also convert Name String[] to int[]
for(int j = 2 ; j < store_value.length ; j++)
{
int aInt[j] = Integer.parseInt((String)s tore_value [j]);
}
for(int j = 2 ; j < store_value.length ; j++)
{
int aInt[j] = Integer.parseInt((String)s
}
ASKER
for(int j = 2 ; j < store_value.length ; j++)
{
int aInt[j] = Integer.parseInt((String)s tore_value [j]);
}
=============Above mention code doesn't work
{
int aInt[j] = Integer.parseInt((String)s
}
=============Above mention code doesn't work
>>=============Above mention code doesn't work
In what way '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...
- 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)s tore_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];
for(int j = 2 ; j < store_value.length ; j++)
{
aInt[j] = Integer.parseInt((String)s
}
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)s tore_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];
for(int j = 2 ; j < store_value.length ; j++)
{
aInt[j-2] = Integer.parseInt((String)s
}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)s tore_value [j]);
Good point
Good point
ASKER
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,Elevat ion};
---------------------
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_Calc ulation();
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
}
.........
...............
...............
}
Under my MyTable Class , I have a method "Object[] Row_Column_Calculation()"
which will return value
return new Object[]{State_Name,Elevat
---------------------
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_Calc
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
}
.........
...............
...............
}
>>int aInt[j] = Integer.parseInt(Elevation [j]); // Need to fix this ..getting error
You're not listening... ;-)
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.
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
}
If it still fails, post the errors.
ASKER
Able to compile without error
So everything fixed now?
ASKER
I am getting error like while running the code
at java.lang.NumberFormatExce ption.forI nputString (NumberFor matExcepti on.java:48 )
at java.lang.Integer.parseInt (Integer.j ava:456)
at java.lang.Integer.parseInt (Integer.j ava:497)
at java.lang.NumberFormatExce
at java.lang.Integer.parseInt
at java.lang.Integer.parseInt
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[].
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
ASKER
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[]
So Class B will not have to convert the String[] to int[]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
values like 34.5,89,1,2,34.9,56.2 ,9.2,8.3 are there in String[] Elevation
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Do I also have to change from float to int explicitly ?
ASKER
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
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(Elevat ion[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...
With some type of rounding?
If so something like this may be an answer:
aInt[j] =(int) (Double.parseDouble(Elevat
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...
ASKER
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
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
>>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 :)
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.
int aInt[j] = Integer.parseInt((String)s