I was also instructed to create the Scanner object with Try with Resources. I get that.If your method fails to read the file, don't catch the exception. Declare it with a throws clause and let the calling method deal with it.
Fred Kleinschmidt wrote:I would not have placed the read statements in a try-catch the way this code did.
The problem with their way is that you cannot tell whether the program failed to open the file, or whether the open was successful but the file was empty. I would have p;laced the try-catch in the main(), and handle any errors in that catch block.
I would have declared the vowels variable as static final, since it would be the same regardless of how many instances of WordInspection were created.
All things are lawful, but not all things are profitable.
All things are lawful, but not all things are profitable.
I presume you are reading from a file; you must therefore make sure to close the Scanner whatever happens, and try‑with‑resources is usually the best way to do that. The only source for which you don't close a Scanner is System.in.Lisa Austin wrote:. . . If your method fails to read the file, don't catch the exception. . . . create the Scanner object with Try with Resources. I get that.
That is exactly what the MOOC code is doing; the private readWords() method throws an exception and the calling. . . not catching the exception and instead declare it with a throws clause . . .
Another very nice distinction, but I think you are probably correct to reduce the scope of vowels and reduce the size of the object. Yes, make it a final local variable.. . . why [is] "vowels" . . . a final field? . . . Who is right here? Me or MOOC ? Or does it matter?
That's a pleasureThanks for any help. . . .
Knute Snortum wrote:My feeling about a try/catch in main() that just prints the stack trace is that it's not doing anything useful. Why not let the error "bubble up" to the top? Alternatively, just catch the new Scanner line (20) and throw a new error that is specific to the method -- with the file name, for instance.
Knute Snortum wrote:This is not related to your question, but any method called from a constructor should be final.
Campbell Ritchie wrote:
Lisa Austin wrote:. . . If your method fails to read the file, don't catch the exception. . . . create the Scanner object with Try with Resources. I get that.
I presume you are reading from a file; you must therefore make sure to close the Scanner whatever happens, and try‑with‑resources is usually the best way to do that. The only source for which you don't close a Scanner is System.in.
Campbell Ritchie wrote:
It is usually not possible to go chasing files in the read() method, so you would usually pass the buck back to the method calling read(), which you can do best with an Exception.
That is exactly what the MOOC code is doing; the private readWords() method throws an exception and the calling. . . not catching the exception and instead declare it with a throws clause . . .
methodconstructor handles it.
Campbell Ritchie wrote:
I don't like the use of plain simple Exception, particularly since you can predict in advance what sort of exception can or can't be thrown. I would put the call to readWords() into a loop. I would be more specific about what sort of exception to handle. And I wouldn't create a new List in line 15, though other people maybe would.
Campbell Ritchie wrote:
You appear to have found some old code; it is written in a Java5/6 style, with no NIO classes, nor try‑with‑resources.
Campbell Ritchie wrote:
That is one way to do it; there are lots of other ways. The extra {} are there to restrict the scope of the local variables.
There are lots of other ways to find a file than file chooser; unfortunately it returns a File object, which I have converted to a Path.
The needsReading variable is true until either readFile() completes without an Exception or you click something other than “Open” on the file chooser; that terminates the loop.
I prefer hasNext() and next() because it is possible to get an empty line, but not an empty “next”, with the standard delimiter. Also next() deals with the problem of several words on the same line. But that is a nice distinction.
Campbell Ritchie wrote:
Another very nice distinction, but I think you are probably correct to reduce the scope of vowels and reduce the size of the object. Yes, make it a final local variable.. . . why [is] "vowels" . . . a final field? . . . Who is right here? Me or MOOC ? Or does it matter?
That's a pleasureThanks for any help.
. . .
Campbell Ritchie wrote:
Alternative form for that method suggested, which you can use for several of those methods:-I shall let you work out its details. You can change readFile() similarly:-
So I'm not familiar with what "bubble up" to the top means. I did some Googling and is this a good example of what that means?
https://teamtreehouse.com/community/whats-meant-by-bubbling
All things are lawful, but not all things are profitable.
The word "token" in the description kind of throws me. What does the word "token" in the javadoc exactly mean? I took it as each word = a token.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:
So I'm not familiar with what "bubble up" to the top means. I did some Googling and is this a good example of what that means?
https://teamtreehouse.com/community/whats-meant-by-bubbling
That's what I meant. It probably isn't that greatest term for what I'm describing since "bubble up" is often used about the same action with events, but I can't think of a better term. "Propagate"? Would that have been clearer?
I think that is a bad example of anything. For a start they are intentionally throwing an unchecked exception. The theory behind unchecked exceptions is that the only thing you can do to prevent it is to change the code somehow. At least that is the theory; it isn't always true.Lisa Austin wrote:. . . I'm not familiar with what "bubble up" to the top means. I did some Googling and is this a good example of what that means? . . .
Follow the Winston Gutkowski school of computing. Make everything in sight final, and take the final modifier off if you have any problems.Lisa Austin wrote:. . . I'm still trying to wrap my head around WHEN to make something final . . . .
No, I don't think it is.Lisa Austin wrote:. . . Is this what is called "bubbling up" which Knute Snortum mentioned?
It is whatever is separated by the delimiter. Since the default delimiter for a Scanner is whitespace, it means any visible bit of text separated from the rest of the text by whitespace. Remember line end sequences count as whitespace. If you write multiple words separated by spaces, then each word is a token, as defined before. The Scanner#tokens() method does something different, but each Stream element is a token. Scroll that link to the top of the page; it gives some examples (mostly drawn from Dr Seuss) about what a token looks like.. . . What does the word "token" in the javadoc exactly mean?
Well, that is my opinion, but I am completely correct. . . post about beginners needing to be familiar with Streams . . .
Winston recommends making everything in sight final and private. The tighter encapsulation makes for more reliable code. If you have any problems, you can always change the modifiers. You can change private→public, but you can't change public→private without risking breaking dependent code.Yesterday, I wrote: . . . the Winston Gutkowski school of computing. . . .
Campbell Ritchie wrote:
No, I don't think it is.Lisa Austin wrote:. . . Is this what is called "bubbling up" which Knute Snortum mentioned?
It is whatever is separated by the delimiter. Since the default delimiter for a Scanner is whitespace, it means any visible bit of text separated from the rest of the text by whitespace. Remember line end sequences count as whitespace. If you write multiple words separated by spaces, then each word is a token, as defined before. The Scanner#tokens() method does something different, but each Stream element is a token. Scroll that link to the top of the page; it gives some examples (mostly drawn from Dr Seuss) about what a token looks like.. . . What does the word "token" in the javadoc exactly mean?
Well, that is my opinion, but I am completely correct. . . post about beginners needing to be familiar with Streams . . .
Some suggestions about how you might handle exceptions here, as threatened last night.
Campbell Ritchie wrote:
Winston recommends making everything in sight final and private. The tighter encapsulation makes for more reliable code. If you have any problems, you can always change the modifiers. You can change private→public, but you can't change public→private without risking breaking dependent code.Yesterday, I wrote: . . . the Winston Gutkowski school of computing. . . .
Yes, you haveLisa Austin wrote:. . . Got it. . . . .
Campbell Ritchie wrote:
Yes, you haveLisa Austin wrote:. . . Got it. . . . .
It's private, so that is all right. I think it should be static, too because it does nothing with any of the object's fields. Slightly odd bit of design whichever way you look at it.Knute Snortum wrote:. . . any method called from a constructor should be final.
Lisa Austin wrote:. . . . Is this what is called "bubbling up" which Knute Snortum mentioned? . . .
I wrote:No.
Hahahahahahahahahahahahahahaha!To all intents and purposes, Knute wrote:Yes.
All things are lawful, but not all things are profitable.
Campbell Ritchie wrote:. . . even to know better than Knute does what he means?
I usually say, “propagate.”
All things are lawful, but not all things are profitable.
And what did Salvin and Stephan say in this thread?Knute Snortum wrote:. . . Propagate it is then . . .
All things are lawful, but not all things are profitable.
I love a woman who dresses in stainless steel ... and carries tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|