Norm Radder wrote:Does the method return the value of searchItem or null? If so, should the method be boolean?
Norm Radder wrote:That sounds like a description of a method that should return either true or false.
bryant rob wrote:I see that the code above's purpose is to provide a
way for the code below to know if searchItem exists or not. But, it does not look to me as though the code above returns a true or false, but rather an index position of the item if it exists, and if it doesn't a null. Yes or no?
Liutauras Vilda wrote:
No, it doesn't return an index position. What (which line(s) of code) makes you think it returns an index?
bryant rob wrote:Line 3 of the original code: .indexOf(searchItem)
Someone once wrote:The two hardest problems in computer programming are 1) cache invalidation, 2) naming things, and 3) off-by-one errors
bryant rob wrote:in cases like this should I use the else or not
Junilu Lacar wrote:2. integer hasA() - "has a" implies a question answerable by yes or no which again brings us back to contains() being sufficient for this purpose.
Junilu Lacar wrote:Since this is a very short method, most coding conventions allow foregoing the else part. There are many ways to skin this cat:
Another option would be to throw an exception of some sort, like an ItemNotFoundException of your making. That could (probably would) lead you to a discussion about checked vs. unchecked exceptions (But please, let's not digress into that discussion here; I just wanted to make a point that there are many design choices from which to choose, the merits of each being up for discussion and lively debate).
Junilu Lacar wrote:
Another option would be to throw an exception of some sort, like an ItemNotFoundException of your making.
bryant rob wrote:can anyone agree on anything in java?
Paul Clapham wrote: If you want to read more (a lot more) about that sort of thing you can check Wikipedia: Indentation style.
Paul Clapham wrote:
Tim Holloway wrote:However, the brace-on-its-own-line is perhaps more readable. I'm not going to worry about that one. Especially since printing is a last-resort option for me these days.
There is a class of program called a "pretty-printer" than can take program text and apply a set of formatting rules to it. Also many of today's IDEs have their own formatters, and if you don't like their standard arrangements, there's usually a style preferences dialog you can bring up.
That is because the old Sun Style Guide recommended such indentation. There is nothing to stop you finding the options in your IDE's formatter and enabling the one to put all braces on lines by themselves. (That doesn't work too well for array initialisers ) And why are you worrying about printing your code? Who uses paper for coding any more?
bryant rob wrote:. . . Intellij formats in this manner. . . .
Campbell Ritchie wrote: There is nothing to stop you finding the options in your IDE's formatter and enabling the one to put all braces on lines by themselves. (That doesn't work too well for array initialisers )
Campbell Ritchie wrote: Who uses paper for coding any more?
Campbell Ritchie wrote:And why are you worrying about printing your code? Who uses paper for coding any more?
I have never done that. I have however written the logic of the steps on a sheet of paper and cut the sheet into ½in wide strips. Sometimes I have to duplicate those strips. It was really good for implementing truly demonic choice. The only problem is that my implementation of
Tim Holloway wrote:. . . I have to use a highlighting pen . . ..
occupies about 1,440 lines of output code.
A ⊓ B ⊓ C ⊓ D ⊓ E ⊓ F ⊓ G ⊓ H
bryant rob wrote:Me. It's hard for me to trace and check off issues that I am having on a computer screen, and understand the code. Plus, I mainly print the code out and make notes along the way so that I can learn from my mistakes, compare the original way I did something to how I changed the code, etc. Works for me, but I am open to recommendations for an alternative method of learning this.
Junilu Lacar wrote:As for notes, I use apps like Evernote and Joplin (learned about this one from a post another moderator made, I forget who it was now, maybe Tim H?). I hardly ever take notes down on paper anymore.
Tim Holloway wrote:Also, for the initial stages of program design, I like FreeMind, which is a mind-mapper.
Tim Holloway wrote:almost never have to grab for the mouse.
Tim Holloway wrote:No, mind mapping is about capturing ideas, not about logic flows. There are other tools for that.
bryant rob wrote:I took notes on paper with this one.
The user inputs an item into scanner in the Main class searching for the item in the grocery list. item is passed as a parameter as a String to the findItem() in my GroceryList class findItem() returns an integer index position using the indexOf() method and if item is found
Afraid I shall have to agree with Liutauras there. You don't enter items, but text representing their names. I also worry whenever I see people who have the concept of a “Main” class. I think it is unfortunate that NetBeans even creates a class of that name. There is nothing “main” about the class containing a method about which there is nothing “main”. Except that that is its name.
bryant rob wrote:. . .
The user inputs an item into scanner in the Main class searching for the item in the grocery list.. . .