• Post Reply Bookmark Topic Watch Topic
  • New Topic

Traverse an ArrayList to search for an integer key  RSS feed

 
Coley Avett
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very new to coding and I am struggling with one of my intro to Java Projects. This project involves creating a GUI that has a list of trees and their possible heights, giving each tree a range for their possible minimum and maximum heights.

I am having trouble creating this method for the TreeDatabase (TreeDB class):

public String queryByPossibleHeight(int key)
Instructions for this method are as follows: this method creates and returns a single string that contains the trees that can be found such that the value key is in the range for possible height. Traverse the entire ArrayList of Trees and for each one of the trees contained in the ArrayList invoke the method inRange() from the class Tree. Pass to the method inRange() the parameter key. If inRange() returns true, invoke the method toString() from the class Tree. Append the value returned by invoking the method toString() to the String result, adding a “\n” at the end. Once the entire ArrayList has been traversed, return the String result.

My 3 classes so far (I haven't gotten to the actual GUI yet):







As you can see in the last TreeDB class, in the public String queryByPossibleHeight(int key) method I have something wrote in, but I was just testing random things and actually have no idea what to do. Can anyone help?

Thank you!
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably this doesn't compile?

getInRange is not a method in the Tree class.
 
Coley Avett
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. I tried using just range, but that doesn't compile either.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please TellTheDetails (<- click) of the question you have. What exactly is going wrong?

I didn't see a main() method. How are you testing this?

Also, the Tree class should be using Range's inRange() method, not re-coding it.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you made any progress?

And welcome to the Ranch
 
Jon Dusnik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Watch the format of your code. I'm doing this same assignment and I was stuck on the same stupid problem for 3 hours. I would ignore declaring the string "thisInRange". I used a while loop to traverse my list by for your if statement, i would try "if (thisTree.inRange(key) == true)" this will specify and integer in your method "inRange". Just my thoughts but I am new to coding as well so I'm really sorry if that doesnt work!
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Jon!

Jon Dusnik wrote:(..) i would try "if (thisTree.inRange(key) == true)"

Just a little nitpick.
It would be better to just use if (thisTree.inRange(key))
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Dusnik wrote:i would try "if (thisTree.inRange(key) == true)"

Never compare a boolean expression with a boolean literal. It's redundant, and also error prone. Just use if (thisTree.inRange(key))
 
Jon Dusnik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Good to know!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!