# Returning an array of indexes in my program.

• April 16th, 2013, 02:09 PM
Spanky_10
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:

Code Java:

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+")");
}
}

• April 16th, 2013, 02:23 PM
Norm
Re: Returning an array of indexes in my program.
Quote:

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:
Code :

public DataType[] returnAnArray() {
DataType[] theArray =  new DataType[theSize];
...
return theArray;   //  return the array
}

• April 16th, 2013, 02:34 PM
Spanky_10
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

Code Java:

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 :(
• April 16th, 2013, 02:35 PM
Norm
Re: Returning an array of indexes in my program.
Quote:

Would this work?
Compile and execute it and see what happens.
• April 16th, 2013, 02:46 PM
Spanky_10
Re: Returning an array of indexes in my program.
This is what I compiled
Code Java:

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
• April 16th, 2013, 03:21 PM
Norm
Re: Returning an array of indexes in my program.
Quote:

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.