• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Help with Arrays

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic