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 6 of 6

Thread: Returning an array of indexes in my program.

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Returning an array of indexes in my program.

    I have a program that creates a double array for 50 states storing the last 10 tax rates for each year. The tax rate is less than .06 in all of them and is randomly configured to make the program run easier. This program is just to help me learn to access multidimensional arrays.

    I need to create the following:
    A method returning an array of indexes of the states that have had at least one year with a tax rate less than 0.001

    I created a method that returns the max tax rate of all the states and years and also the least and returns the indexes.
    However, I do not know how to return an array of indexes to satisfy this question.

    Here's my class so far:

    import java.text.DecimalFormat;
    public class SalesTax
    {
        private double [][] rates;
     
        public SalesTax()
        {
            rates = new double [50][10];
     
            for(int x=0; x<rates.length; x++)
            {
                for(int i=0; i<rates[x].length; i++)
                    {
                        double z= Math.random();
                        while(z > .06)
                        {
                            z=Math.random();
                        }
                        rates[x][i] = z;
                    }
            }
        }
     
        DecimalFormat newForm = new DecimalFormat("0.000000");
        public void get_largest_rate()
        {
           double max = rates[0][0];
           int tmpI = 0;
           int tmpX = 0;
     
           for(int x = 0; x < rates.length; x++)
           {
               double[] inner = rates[x];
               for (int i = 0; i < rates[x].length; i++)
               {
                  if(inner[i] > max)
                  {
                      max = inner[i];
                      tmpX = x;
                      tmpI = i;
                  }
               }
           }
     
           System.out.println(newForm.format(max));
           System.out.println("The index of the highest rate is: ("+tmpX+","+tmpI+")");
        }
     
        public void get_less_tax()
        {
            double leastTax = rates[0][0];
            int tmpX = 0;
            int tmpI = 0;
            for(int x = 0; x < rates.length; x++)
            {
               double[] inner = rates[x];
               for (int i = 0; i < rates[x].length; i++)
               {
                   if(inner[i] < leastTax)
                   {
                        leastTax = inner[i];
                        tmpX = x;
                        tmpI = i;
                   }
               }
            }
     
            System.out.println(newForm.format(leastTax));
            System.out.println("The index of the lowest rate is: ("+tmpX+","+tmpI+")");
        }
    }


  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: Returning an array of indexes in my program.

    how to return an array
    Make the return type for the method be an array and use a return statement with the name of the array:
    public DataType[] returnAnArray() {
       DataType[] theArray =  new DataType[theSize];
       ...
       return theArray;   //  return the array
    }
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array of indexes in my program.

    I want to use a for loop to check if the number stored in each part of the array is less than .001

    for(int x = 0; x < rates.length; x++)
            {
               for (int i = 0; i < rates[x].length; i++)
               {
                   if(rates[x][i] < .001)
                   {
                        double[] array1 = new double[rates.length];
                        for(int j = 0; j < rates.length; j++)
                        {
                            for(int h = 0; h < rates[j].length; h++)
                            {
                                rates[x][i] = array1[j][h]
                            }
                        }
     
                        return array1;
                   }
    }

    Would this work? I do not have my program on the computer I am using to try it out

  4. #4
    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: Returning an array of indexes in my program.

    Would this work?
    Compile and execute it and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array of indexes in my program.

    This is what I compiled
    public double[][] get_less_tax()
        {
            double leastTax = rates[0][0];
            double[][] array1 = new double[rates.length][];
            for(int x = 0; x < rates.length; x++)
            {
                double[] inner = rates[x];
               for (int i = 0; i < rates[x].length; i++)
               {
                   if(inner[i] < .001)
                   {
                        for(int j = 0; j < rates.length; j++)
                        {
                            for(int h = 0; h < rates[j].length; h++)
                            {
                                rates[x][i] = array1[j][h];
                            }
                        }
                   }
                }
            }
            return array1;
        }

    I got:
    java.lang.NullPointerException
    at SalesTax.get_less_tax(SalesTax.java:71)
    at SalesTaxTest.main(SalesTaxTest.java:14)

    when I ran my tester program. I don't know where my mistake is

  6. #6
    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: Returning an array of indexes in my program.

    java.lang.NullPointerException
    at SalesTax.get_less_tax(SalesTax.java:71)
    There is a variable with a null value on line 71. Look at line 71 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 71 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Returning an array
    By maple1100 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: January 21st, 2013, 02:38 PM
  2. Returning An Array
    By LoganC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2012, 03:57 PM
  3. [SOLVED] Issue when returning an array
    By Broxxar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 21st, 2012, 10:19 PM
  4. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 04:30 AM
  5. Assgining values to array indexes
    By chronoz13 in forum Collections and Generics
    Replies: 3
    Last Post: December 28th, 2009, 11:09 PM