# Conversions of Numbers in Arrays

• August 27th, 2010, 09:04 PM
KiwiFlan
Conversions of Numbers in Arrays
For one of my programs, I have to convert the wind speed from knots to mph. The variable, knots, is a double. The other variable, wind, is a double [], otherwise an array. How would I convert what's in wind array from knots to mph?

Thanks.
• August 28th, 2010, 07:20 AM
Norm
Re: Conversions of Numbers in Arrays
Quote:

How would I convert what's in wind array from knots to mph?
Do you have the definition of a knot? A nautical mile per hour.
A nautical mile is approximately 6000 ft.
mph is a standard mile per hour.
A standard mile is 5280 feet.
Compute the ratio and use that value to convert.
• September 1st, 2010, 06:14 PM
KiwiFlan
Re: Conversions of Numbers in Arrays
The problem I'm having is converting from an array to a double.
• September 1st, 2010, 06:40 PM
helloworld922
Re: Conversions of Numbers in Arrays
If you have an array of doubles, you should be operating on every element of the array.

Code Java:

```for(int i = 0; i < myArray.length; ++i) { // multiplies every element in myArray by 1.5 myArray[i] = myArray[i] * 1.5; }```
• September 1st, 2010, 07:05 PM
Norm
Re: Conversions of Numbers in Arrays
Quote:

The problem I'm having is converting from an array to a double.
That doesn't make sense to me.
An array is a way to store and access many elements of data of the same type. You can have an array that holds any data type.
double is a data type. String is a data type. Both can be stored in an array.

Can you explain what is in the array and how you want to convert an array to a double?
• September 1st, 2010, 07:41 PM
KiwiFlan
Re: Conversions of Numbers in Arrays
The conversion problem is solved. :) However, I'm having a new problem. This is my code below. For some reason, I'm getting this error:

"java.lang.ArrayIndexOutOfBoundsException: 59
at Hurricanes2.main(Hurricanes2.java:56)"

and it highlights the very first "if" statement.

Code Java:

```import java.util.Scanner; import java.io.File; import java.io.IOException; public class Hurricanes2 { public static void main(String[] args) throws IOException { Scanner inFile = new Scanner(new File("hurcdata2.txt")); int numLines = 0;   while(inFile.hasNextLine()) { inFile.nextLine(); numLines++; } inFile.close(); inFile = new Scanner(new File("hurcdata2.txt"));   int [] years = new int[numLines]; String [] name = new String [numLines]; int [] category = new int [numLines]; int [] pressure = new int [numLines]; double [] wind = new double [numLines]; String [] month = new String [numLines];   System.out.printf("%45s\n", "Hurricanes 1980 - 2006"); System.out.println(); System.out.println("Year Hurricane Category Pressure (mb) Wind Speed (mph)"); System.out.println("=======================================================================");   int i=0; while(inFile.hasNext()) { years[i] = inFile.nextInt(); month[i] = inFile.next(); pressure[i] = inFile.nextInt(); wind[i] = inFile.nextDouble(); name[i] = inFile.next(); i++; } inFile.close();   for(int m = 0; m < wind.length; m++) { wind[m] = (int)((wind[m] * 1.15) * 10 + 0.5)/10.0; }   if(wind[i]>=74 && wind[i]<=95) { category[i] = 1; } else if(wind[i]>=96 && wind[i]<=110) { category[i] = 2; } else if(wind[i]>=111 && wind[i]<=130) { category[i] = 3; } else if(wind[i]>=131 && wind[i]<=155) { category[i] = 4; } else { category[i] = 5; }   for(int n = 0; n < years.length; n++) { System.out.printf("%1d %12s %25d %25f %25\n", years[n], name[n], category[n], pressure[n], wind[n]); }   } }```
• September 1st, 2010, 07:59 PM
Norm
Re: Conversions of Numbers in Arrays
Quote:

java.lang.ArrayIndexOutOfBoundsException: 59
at Hurricanes2.main(Hurricanes2.java:56)"
At line 56 of your program there is an array that is indexed by a value greater then the size of the array. The index was 59.

Look at line 56, find the array, and see how your code tried to use an index with the value of 59 there.