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.

Results 1 to 7 of 7

Thread: Conversions of Numbers in Arrays

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Conversions of Numbers in Arrays

    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.

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Conversions of Numbers in Arrays

    The problem I'm having is converting from an array to a double.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Conversions of Numbers in Arrays

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

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    KiwiFlan (September 1st, 2010)

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Conversions of Numbers in Arrays

    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?

  7. #6
    Junior Member
    Join Date
    Jul 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    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]);
            }
     
        }
    }

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Conversions of Numbers in Arrays

    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.

Similar Threads

  1. conversions and excel
    By mkslt4 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 10th, 2010, 01:54 AM
  2. Roman Numbers
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 12th, 2009, 02:19 AM
  3. Performance of Type Casting and Conversions
    By fritzoid in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2009, 07:56 PM
  4. Java Variable Conversions
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: June 23rd, 2009, 05:03 AM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM