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.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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.
Do you have the definition of a knot? A nautical mile per hour.How would I convert what's in wind array from knots to mph?
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.
The problem I'm having is converting from an array to a double.
If you have an array of doubles, you should be operating on every element of the array.
for(int i = 0; i < myArray.length; ++i) { // multiplies every element in myArray by 1.5 myArray[i] = myArray[i] * 1.5; }
KiwiFlan (September 1st, 2010)
That doesn't make sense to me.The problem I'm having is converting from an array to a double.
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?
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.
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]); } } }
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.java.lang.ArrayIndexOutOfBoundsException: 59
at Hurricanes2.main(Hurricanes2.java:56)"
Look at line 56, find the array, and see how your code tried to use an index with the value of 59 there.