This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

2d Array problem gasprices

rafeeq khan
Greenhorn
Posts: 6
A consumer protection group who is concerned about these wild swings in gasoline prices has asked you to write a simple Java program that let them record the prices of gasoline at various stations through the city and then display them in a table format. The group leader sketches out what she would like to see on the displayed data:

Each gasoline station will have an ID number and this will be entered in the first column. The morning price will be entered in the second column, and the Evening Price will be entered in the third column.
MILD VERSION Specifications for GasPrices.java

Your program should do the following:
1) Print a short description to the screen that this program will record gas station ID�s and the morning and evening prices for each gas station.

2) You will store the data in a 2D array called gasPriceArray. The number of columns in the 2D array will be three, but the program will ask the user how many gas stations they will be entering. Use this value to set the number of rows in your 2D array.

3) Your program will then loop and ask the user to enter first the gas station ID, then the morning price, and then the evening price, until you have entered all of the data for your gas stations.

I'm stuck on this please help, I cant seem to display the data, here is my coding so far

[B]
[/B]

it does loop, I think it takes the arrays into memory but cant seem to display it, any help will be much appreciated.

this is the output:

3
1001
101.2
103.2
1002
99.9
100.9
1003
110.9
15.8
Station ID: [D@1a758cb103.2
Station ID: [D@1b67f74100.9
Station ID: [D@69b33215.8

[ January 21, 2008: Message edited by: fuka fuka ]
[ January 21, 2008: Message edited by: fuka fuka ]

Henry Wong
author
Marshal
Posts: 21743
85
Take a look at this line:

Hint: Is the station id really a whole row? Or is it a particular column of a row?

Henry

rafeeq khan
Greenhorn
Posts: 6
I still can't make it, please can you tell me where should I enter the code you told me.

thanks

rafeeq khan
Greenhorn
Posts: 6
station id is particular column of a row(say first column)

rafeeq khan
Greenhorn
Posts: 6

cant get the station id to show, it shows some garbage

Campbell Ritchie
Sheriff
Posts: 50702
83
It's not showing garbage, it's showing something you are unfamiliar with. The bit about [D means that it is an object of type array-of-doubles and the @ is an @ sign and the bit afterwards is the hashCode; if you look in the API for the Object class and find its toString() method specification you find that is what it prints.

What it means is that you have managed to get your hands on the ith array, when you thought you had got the jth member of the ith array.

There is not actually such a thing in Java as a two-dimensional array. Have you read the Java tutorial about arrays (push ctrl-F and look for [][])?

Can't give you any more hints, I am afraid. Henry Wong has already given you a very useful hint.

rafeeq khan
Greenhorn
Posts: 6
thanks guys I appreciate your help very much

Phyo Pine
Greenhorn
Posts: 5
I think you are confused about 2D array. And I don't see the reason for the second for-loop either ( Unless you want afternoon price or whatever).

rafeeq khan
Greenhorn
Posts: 6
Thanks a lot Pine