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
posted
0
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
posted
0
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
posted
0
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.
John [ January 26, 2007: Message edited by: John Bartlett ]
joanne robinson
Greenhorn
Joined: Jan 24, 2007
Posts: 15
posted
0
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
posted
0
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
posted
0
a) It has to be "c.getReg()" instead of "Car.getReg()" to call the getReg()-method you desire... b) Think about - what is "r"?
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: .get method data from an array using simple IO