aspose file tools*
The moose likes Beginning Java and the fly likes 2d Array problem gasprices Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "2d Array problem gasprices" Watch "2d Array problem gasprices" New topic
Author

2d Array problem gasprices

rafeeq khan
Greenhorn

Joined: Jan 21, 2008
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:

Please enter number of stations:
3
Please enter Station ID:
1001
Please enter morning price:
101.2
Please enter evening price:
103.2
Please enter Station ID:
1002
Please enter morning price:
99.9
Please enter evening price:
100.9
Please enter Station ID:
1003
Please enter morning price:
110.9
Please enter evening price:
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
Sheriff

Joined: Sep 28, 2004
Posts: 18716
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
rafeeq khan
Greenhorn

Joined: Jan 21, 2008
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

Joined: Jan 21, 2008
Posts: 6
station id is particular column of a row(say first column)
rafeeq khan
Greenhorn

Joined: Jan 21, 2008
Posts: 6


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

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Jan 21, 2008
Posts: 6
thanks guys I appreciate your help very much
Phyo Pine
Greenhorn

Joined: Jan 22, 2008
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

Joined: Jan 21, 2008
Posts: 6
Thanks a lot Pine
 
jQuery in Action, 2nd edition
 
subject: 2d Array problem gasprices