• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

help with display routine

 
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your displayFlowers method is still wrong. Have you tried tracing through that code by hand? By tracing through by hand, I mean you get a piece of paper and a pen and pretend you're the computer. Then you execute each command and track the state of your variables manually on the piece of paper. You won't know what's wrong until you learn to understand what's happening when the computer executes the commands you gave it. Only when you try to perform the commands yourself will you understand why the computer appears to be doing the wrong thing: it's because you told it to do the wrong thing.

Ok, I take that back. It does look like you implemented the algorithm that Carey suggested. I just misunderstood what you were doing in the first loop.
 
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Also, your logic for searching for a flower is all wrong. Let's ignore that you're not accounting for nulls in the array for now and assume that the array will be full of valid Flower objects. The logic in the loop basically looks at each flower and immediately prints out either "Flower found!" or "Flower not found."  So if you have 25 flowers that don't match the flower being searched for, that code will print "Flower not found." 25 times.  Is that really what you want?



So maybe something like an if statement staying if user input is not equal to any flower in the array, then print out "Flower not found"?
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Wages wrote:So maybe something like an if statement staying if user input is not equal to any flower in the array, then print out "Flower not found"?


Unfortunately, guessing is not a good strategy for figuring out the correct logic. You can experiment but in the end, you need to understand exactly what you are telling the computer to do. The first step in doing that is working out the logic as though you're instructing your grandmother who knows nothing about computers. Think about what you would tell her to do and the exact stepwise instructions you'd give to her to follow.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

he first step in doing that is working out the logic as though you're instructing your grandmother who knows nothing about computers. Think about what you would tell her to do and the exact stepwise instructions you'd give to her to follow.




I'll 100% give it a shot.  I understand most logic, but somethings leave me hanging.  I'm reading the site posted earlier for a refresher.  I just want to get caught up in my Java class so I can continue refreshing and trying to find little projects to build my Java confidence.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm also having trouble understanding why a copy was needed.



 
Marshal
Posts: 8387
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still looking to your addFlower() method, after you add flower you increment 'count' instance variable to follow how many flowers you have.
Now when you remove flower, you don't decrement same counter.

And if you'd nicely remove that flower without leaving 'null' in between any flower (you do something there with arraycopy, but I didn't check what you exactly do there, as there is one variable I can't wrap my head what it represents -  numElts, agh, must be numberOfElements), everything would become easier in later methods I think, as you could rely on counter, which supposed to be a current amount of flowers you have within array, of course if that was your current idea.

But it got very messy as there are lots of commented code, I'm not sure which is the most recent version and which methods contain issues.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
But it got very messy as there are lots of commented code, I'm not sure which is the most recent version and which methods contain issues.



The most current version is the un-commented code.
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Wages wrote:I'm also having trouble understanding why a copy was needed.



Honestly, I'm having trouble understanding what that code is even doing at all.  I'm seeing a common pattern in your code that indicates you don't quite understand how the if-statement inside a for-loop works.  Just to clarify: The if-statement will be executed on every iteration of the for-loop. So, assuming your use of System.arraycopy() is even correct, if your for-loop looks at 25 flowers, it could potentially remove all the flowers and print "Flower removed!" 25 times. If none of the flowers match the search criteria, then your code will print out "Flower was not found." 25 times.  If there were only a few matches, then you'd get a mix of "Flower removed!" and "Flower was not found." messages totaling 25 in all.  I doubt that's what you want.

Maybe your problem is that you think you have to do everything inside the for-loop.  You don't have to, you know.
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing you can do to clean up your code is to eliminate duplication.  There are a lot of repetitious lines of code in your program. Take your use of a Scanner, for example. You don't really need to create a new Scanner object and tie it to System.in every time you call a method. And you don't have to keep writing System.out.println("some prompt"); input.nextLine();.  If you extract that duplication out to one place, your code will be much clearer and cleaner.


You don't have to make your prompts very robotic either. Compare "What flower do you want to find?" versus "Enter the flower you want to search: "
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

You don't have to make your prompts very robotic either. Compare "What flower do you want to find?" versus "Enter the flower you want to search: "



I'm not sure I'm following how the promptLine() works.  You have to declare variables instead of just using a setter to set the name?  Does it still work with setters?  
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both promptLine() and promptWord() return String objects, whatever is returned by input.nextLine() (line 13) and input.next() (line 8), respectively.

Given those method definitions, this code:

is the same as writing:


What's your confusion about this?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:What's your confusion about this?




I'm trying to come up with a way that I'd still be using setName() and promptWord().  Like using my addFlower() method.

 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Wages wrote:

Junilu Lacar wrote:What's your confusion about this?


I'm trying to come up with a way that I'd still be using setName() and promptWord().  Like using my addFlower() method.


A method call that returns a value is just like any other expression that has a value. There's nothing special or magic about it.

Instead of writing this repetitious and tedious code:

You can just write

Is there anything that you're not sure about that code?
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what happens with that one line of code:

flower[i] is a reference to a Flower object.  

.setName(...) invokes the Flower object's setName() method.

before you can call setName, the argument has to be evaluated.

The argument is promptWord("What's the flower's name? ")

So, the method promptWord() is called with an argument of "What's the flower's name? " received in the method as its message parameter.

The promptWord() prints out the message parameter.  Then it waits for the user to enter a word.

The word that the user enters is returned to the caller and the promptWord() method terminates.

Execution goes back to where we left off before. We were just about to call the setName() method on a Flower object. We got the result of the promptWord() call back, so we pass that to the setName() method.

The setName method sets the name instance variable of the Flower object to whatever it was that was passed in, that's the name that the user entered.

And that's it.

Were you able to follow the flow?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I understand completely.  I tend to overthink things and try to make them more difficult than they actually are..
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I've re-written my class again (I know its probably annoying for you veteran folk, but its part of my process at this point).  I tried to make it cleaner/simpler.  I always really wanted to use the prompWord() method you showed me, but since we haven't gone over anything like that in class, I figured I'd better not do it.  The displayFlower() method and removeFlower() methods are still giving me some trouble.  Right now, the display method isn't exactly where I had it last night.  At one point I had it to where it was infinite looping the first flower entered and the count (eg "Rose's - 1 over and over).  I was up until 4:30 and I sort of just quit for the night.

 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also did try writing down what I want the program to do on paper.  I think I need more practice, but I think I do see the value in it.
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations on your searchFlower() implementation! It actually looks pretty decent.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Congratulations on your searchFlower() implementation! It actually looks pretty decent.



Thanks!

Does that mean everything else is less than decent?
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Wages wrote:Does that mean everything else is less than decent?


If something doesn't work, would you really be comfortable calling it "decent"?

Don't worry, you're getting there.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Jeremy Wages wrote:Does that mean everything else is less than decent?


If something doesn't work, would you really be comfortable calling it "decent"?

Don't worry, you're getting there.



Is there anything you can say to point me in the right direction but not give it away?
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have the right idea about what to do to actually remove a flower. You're just not doing the right thing to figure out which flower to remove. That process is almost identical to searching for a flower, isn't it? To remove a flower, you first have to find it, right? Once you've found it, then you do that thing to remove it from the array.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:You have the right idea about what to do to actually remove a flower. You're just not doing the right thing to figure out which flower to remove. That process is almost identical to searching for a flower, isn't it? To remove a flower, you first have to find it, right? Once you've found it, then you do that thing to remove it from the array.



I think I'll be able to correct it when I get home!
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Wages wrote:I think I'll be able to correct it when I get home!


Go ahead and code out what you have in mind. Then I have a little challenge for you to clean up your code a little bit more with respect to duplication.  If you do what I suspect you're going to do, you'll have code in your removeFlower() method that looks a lot like the code you have in your searchFlower() method. But let's not get ahead of ourselves. Let's see what you come up with first. It's always easier to write better code once you see the code that's not so good.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops, forgot to add my [/code].  Please help me out with that!
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to think about that logic a little bit more. I said the logic would be almost identical, not exactly identical.  You already had the right code to actually remove a flower before, you just didn't have the right code to find the right one.  So you have to combine the two tasks: first find the right flower to remove, then actually remove it.  Don't just copy and rename a few variables from the searchFlower code. That's not going to work.
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other question for your removeFlower() method is do you want to remove just one match or all the matches? Like if you had four "Rose" in the pack, do you remove all four of them or just the first one you found?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The other question for your removeFlower() method is do you want to remove just one match or all the matches? Like if you had four "Rose" in the pack, do you remove all four of them or just the first one you found?



I think just one occurrence of the flower.

I thought about it more.  I think I have a working removeFlower().  I add a flower, search for it, remove it, search for it, and it returns not in the pack.  When I figure out my display method I'll know for sure.

 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No that's not it. You got from "warmer" to "way colder". It almost looks like you're guessing. You can't guess. You have to understand what the code is doing.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:No that's not it. You got from "warmer" to "way colder". It almost looks like you're guessing. You can't guess. You have to understand what the code is doing.



Am I at least on the right track with the nested for loop?
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you're not. Your search flower method has the correct algorithm, you just have to do something else instead of set a boolean when you've found the flower you're looking for. Not sure why you want to complicate things so much.

In English,

Search flower goes through the flowers one by one and when a match is found, returns a Boolean true.
Remove flower goes through the flowers one by one and when a match is found, removes it.

See how only the last bit is different in the English description?



 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
Search flower goes through the flowers one by one and when a match is found, returns a Boolean true.
Remove flower goes through the flowers one by one and when a match is found, removes it.

See how only the last bit is different in the English description?



Yes, I see the difference.  I have a bad tendency to make things more complicated by overthinking them.  I wish I knew how to stop it.
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Wages wrote:I have a bad tendency to make things more complicated by overthinking them.  I wish I knew how to stop it.


Think in the simplest terms possible. Think in English or whatever language is your primary spoken language. When you've worked out the logic in plain-speak, then translate it to Java-speak.

Here's what I mean:

Search Flowers - for each flower in the pack, check if the flower's name is what we're looking for. If it is, then we've found it and we can stop. If you go through the whole bunch without finding a match, then the flower isn't there.

That's plain and simple to understand, right? There's a hint of Java-speak maybe but it's still language you can use with Grandma who doesn't know anything about computers, much less programming.

Now, you translate that to code/Java-speak:

I only put comments on the code above so you can see how each part of the code relates to the plain-speak version -- I normally will let the code speak for itself. If we take off the comments, is the code less clear?

Anyone who knows enough of Java to be conversant with it will (interpretively) read that code as: "The searchFlower() method returns a boolean, given a name to search for and the array of flowers to search.  For each flower in the pack, if the name we're looking for matches that flower's name, then we're done and return true. Otherwise we keep iterating. If we're done iterating and haven't returned true yet, that means the flower wasn't found, so return false."  

I have marked the parts in that interpretative reading that have a corresponding part in the code.  I underlined "For each" because that's what this construct

is called, a for-each loop.  You read that as "For each flower in the pack" -- pretty straightforward, right?
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, that abbreviated version I just gave you has a little bug in that it won't handle nulls graciously but you already had code from before that knows how to do that, so you make the necessary adjustments. I just wanted to show you the main part of the logic.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never seen a for-each loop.  It's similar to a for loop except it compares a flower object to pack array?  I think?
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Wages wrote:I've never seen a for-each loop.  It's similar to a for loop except it compares a flower object to pack array?  I think?


It's a higher-level version of the regular for-loop. It just goes through anything that is iterable, like an entire array or a Collection (List, Map, Set) without the programmer having to explicitly specify a loop index variable. It's what's called "syntactic sugar" because it makes programming certain kinds of loops "sweeter".  It doesn't compare a flower object to a pack array.  The pack variable is declared as an array of Flower objects. That means you can iterate over that array.  The loop variable is the "each", that is, as you iterate over the array, pack, each Flower in the array will be assigned to the loop variable, flower. In the body of the loop, you can use the flower loop variable to refer to the flower that is currently being iterated over.

These two for-loops are equivalent:

The only reason you need the loop index variable i is so you can use it to access a particular element of the array, but that's really just an implementation detail. The for-each version makes your intent clearer: you're really interested in each flower, not so much the index. In the for-each version, you get down to intent and let Java take care of the details of keeping track of where to start (initializing to 0 to start at first element), which element it currently is looking at (the loop index variable, i), moving to the next element (i++), and when to continue or stop (keep going if i < pack.length).
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic