I was making a Transit Calculator class, and although I have not finished the entire project I wanted to test the two methods that I had already created. The first method runs correctly and gives me the right result, but when it came to my second method which was the “getRidePrices()” method then it runs and gives me a memory address, even though I expected it to give me a double array.
Can someone please check my code for me to see for potential errors in my method. thank you
Here is my code:

``````import java.util.Arrays;
public class TransitCalculator
{
int numDaysTransit;
int numRidesTransit;
String[] options = {"Pay-per-ride", "7-day Unlimited Rides", "30-day Unlimited Rides"};
double[] prices = {2.75, 33.00, 127.0};

public TransitCalculator(int numDays, int numRides)
{
numDaysTransit = numDays;
numRidesTransit = numRides;
}

public double unlimited7Price()
{
if(numDaysTransit%7 != 0)
{
int numRoundedTransit = (numDaysTransit/7) + 1;
double totalPrice = numRoundedTransit * prices[1];
double finalPerPrice = totalPrice/numRidesTransit;
return finalPerPrice;
}
else
{

int nrt = numDaysTransit/7;
double tp = nrt * prices[1];
double fpp = tp/numRidesTransit;
return fpp;
}

}

public double[] getRidePrices()
{
double a = (prices[0])/1;
double b = (prices[1])/7;
double c = (prices[2])/30;

double[] newP = {a, b, c};
return newP;

}
public static void main(String[] args)
{

TransitCalculator yanai = new TransitCalculator(14, 20 );
yanai.unlimited7Price();
System.out.println(yanai.unlimited7Price());
System.out.println(yanai.getRidePrices());

}

}
``````
1 Like

When you try to print an array, you get the memory location. You can iterate through the elements of the array, and print them, or select one to print using it’s index.

``````System.out.println(yanai.getRidePrices()[0]);
``````
2 Likes

you can also try `Arrays.toString(newP)` if you just want to display it the whole set as a string.
deepToString for 2d arrays (not 2d in this case I think).

2 Likes

The Arrays.toString(newP) did not quite work but then I tried it in a different way in which I called on the method from inside the toString like this “Arrays.toString(yanai.getRidePrices()))” then it ran and started working so Thank you.

1 Like

Right! My example implied putting it inside your getRidePrices().

but @midlindner has the more useful answer. Because when you have an array it’s not often useful to get a string return of the entire thing other than for testing (at least in cases that I can think of).

1 Like

Rather than return the array as a string, you could just convert it when you print using the method suggested by @toastedpitabread.

``````System.out.println(Arrays.toString(yanai.getRidePrices()));
``````
1 Like