This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help with Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Arrays" Watch "Help with Arrays" New topic
Author

Help with Arrays

Eric Johns
Greenhorn

Joined: Sep 13, 2003
Posts: 20
I'm writting this program that searches a data file for cars that match the users preferences. I'm having a problem with the line I marked with **, I keep getting a java error that says " cannot resolve symbol" and I don't understand why? anybody have any suggestions, Thanks
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Your "carMatches" array is defined inside of the "for()" loop, but you are trying to reference it after the loop exits. You need to declare it before the "for()" loop, add elements inside the loop, and then you can reference it after the loop ends.
Also, you might want to think about checking for array bounds before you add the next match to the array.
Eric Johns
Greenhorn

Joined: Sep 13, 2003
Posts: 20
Could you explain what you mean by Add Elements? What I'm am trying to do is all car objects that match the Fit requirments will be put into an array, then I need to count all the object that are in my array and then print the results.
Originally posted by Wayne L Johnson:
Your "carMatches" array is defined inside of the "for()" loop, but you are trying to reference it after the loop exits. You need to declare it before the "for()" loop, add elements inside the loop, and then you can reference it after the loop ends.
Also, you might want to think about checking for array bounds before you add the next match to the array.

[ November 13, 2003: Message edited by: Eric Johns ]
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
To add an element to an array is to store something at a particular index of the array.
Your problem is that the array named carMatches is created inside the loop "if ((fitWeight >=80) && (inputCarType.compareTo(type)==0))" and exists only within this loop. You need to create the array ouside of both loops that need to access this array.
Eric Johns
Greenhorn

Joined: Sep 13, 2003
Posts: 20
ok, your guys suggestion work but now I have a new problem. my orginal design was to have the "if ((fitWeight >=80)ect." to add and count matches then drop out of the "for" loop and run a new "for" loop to print my counting results and display the car details. the problem I'm running into is that not every run of the first "for" loop is going to give me a hit so in a sense I am having gap in the array, so when I then try to print my results with the last "for" loop and it run into an array index that has nothing the program crashes. I was wondering if anybody has any suggestions?
[ November 14, 2003: Message edited by: Eric Johns ]
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
You could create an int array then, inside your "if fitWeight" loop, store in this array the index numbers from the car array where you found an appropriate car. Then you could read through this array and fetch each index in turn.
A much better idea would be to print as you go; when you find a match, print the information immediately, then go on with your search.
[ November 14, 2003: Message edited by: Elouise Kivineva ]
Eric Johns
Greenhorn

Joined: Sep 13, 2003
Posts: 20
I was thinking about that too, but it might get to complex because the next step is I have to sort my array by retail price.
Originally posted by Elouise Kivineva:
You could create an int array then, inside your "if fitWeight" loop, store in this array the index numbers from the car array where you found an appropriate car. Then you could read through this array and fetch each index in turn.
A much better idea would be to print as you go; when you find a match, print the information immediately, then go on with your search.
[ November 14, 2003: Message edited by: Elouise Kivineva ]
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
Maybe you'd have an easier time of it if you used Comparators to do all these sortings.
This is what helped me figure out how to use a Comparator.
http://www.cs.queensu.ca/home/cisc124/2001f/slides/slides124set10a.pdf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Arrays
 
Similar Threads
help with IF Statements on a program Newbie
comparing Stings help
help with design of program newbie to java
Question about Exceptions learning newbie
Java help