wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes .get method data from an array using simple IO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ".get method data from an array using simple IO" Watch ".get method data from an array using simple IO" New topic
Author

.get method data from an array using simple IO

joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
Hi all

After creating an array, I now need to pull info back out following a user request in simple IO. After a series of press one for this and that, the user types in a car registratio number. I am not sure how to call the car method and prompt it with the user input. Have I got my ideas completly confused?

This is the code in the main method:



and in the Car class



Please let me know if I have not posted enough code for context.

Thanks in advance

Jo
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Hi,

From reading what you have posted couldnt you just loop through the ArrayList of Car objects and check the getReg() of each car object to see whether it matches the user input and then pull that one back?

e.g.

Im am also a little bit confused to why you are creating another Car instance in the getReg() method in the Car class?

If i have missed something please say and i will try to help again,

John
joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
thanks John, I can see what you are saying and when I do something with the car Ill print it in the console.

I'm getting complier error with ot recognising the variable cars in

[code]
for (Car c : cars) {
[/code/]

which is obviously because its not a variable I am using. However, I'm not really understanding what the for condition is describing for me to assign the correct variable. Could you advise?

Also, regards the additional car1 in car.java - it's just novice error i'm afraid!
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
yeah thats the idea, no problem with making mistakes everyones gotta learn at some time.


This is called a for-each loop, very useful and tidy loop.

read here for better understanding: for-each loop

so,

for ( currentObject : ListOfObjects) {

}

e.g.

Hope this helps?

John
[ January 26, 2007: Message edited by: John Bartlett ]
joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
thank you kindly. All is now understood. My array is referenced as carList so that has worked beautifully.

The compiler is now grumbling about static and non-static, but I notice someone has posted a topic about this, so i will take a look over there before repeating the question.

Thanks again
joanne robinson
Greenhorn

Joined: Jan 24, 2007
Posts: 15
ahhh, it turns out the static/ non static post is a little old and above my head!

So, on the whole, my code seems to be taking better shape, however I'm getting the following compliler error


now, i'd think that I would also have to have the reg as a static variable, but that doesn't seem right as the reg variable is one of many populated throughout the array. Nevertheless, I tried fiddling about with static-ing bits and bobs, to no avail.
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122

a) It has to be "c.getReg()" instead of "Car.getReg()" to call the getReg()-method you desire...
b) Think about - what is "r"?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: .get method data from an array using simple IO
 
Similar Threads
get methods
HELP!
creating a new object in an ArrayList
int and double get methods
constructor to add to an array list