• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scanner Question Again please

 
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,


About two weeks ago I posted this question HERE about scanners.  I've been going through an online course for Java MOOC.   MOOC Website to learn and get more practice with Java.    Unfortunately there isn't really good support with this course which is why I'm here.

In my prior post about Scanners I was told

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.

 I was also instructed to create the Scanner object with Try with Resources.  I get that.

The below code is the MOOC 's suggested Solution not MY solution.  So I have a few questions as to why some things are being done the way it is and I'm wondering if anyone can help me or just give me a reasonable idea?    Like I mentioned , there isn't really good support for the course.  

1.  Regarding what I quoted about not catching the exception and instead declare it with a throws clause and letting the calling method deal with it.  
MOOC's suggestion solution is using a Try / Catch so I assume this isn't the ideal solution.  So what I understand , what I should do is instead declare the constructor with a throws clause and the main method would deal with it.  Is this right?

2.  I don't understand why the String variable "vowels" is declared as a final field?   I would have declared it in the containAllVowels method since it's not used anywhere else but that method.   Who is right here?  Me or MOOC ?  Or does it matter?


Thanks for any help.  





[/code]
 
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.




So for my understanding I created a new WordInspection class to demonstrate what I think you are telling me.    Do I have the right idea?
Also thanks for the explanation on both of my questions.  








 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not related to your question, but any method called from a constructor should be final.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
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.

. . . not catching the exception and instead declare it with a throws clause . . .

That is exactly what the MOOC code is doing; the private readWords() method throws an exception and the calling method constructor handles it.
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.
You appear to have found some old code; it is written in a Java5/6 style, with no NIO classes, nor try‑with‑resources.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.

. . . why [is] "vowels" . . . a final field? . . . Who is right here?  Me or MOOC ?  Or does it matter?

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.

Thanks for any help. . . .

That's a pleasure
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:-
 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



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.

Here is my attempt at the  add a try/catch to the Scanner.  Please let me know how I did.



 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:This is not related to your question, but any method called from a constructor should be final.



I appreciate the extra information.  I'm still learning so this is good to know.   I've updated my WordInspection's readWords method.   By all means PLEASE tell me things like this.

 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



That's something I didn't notice until you said this. The MOOC's suggested solution doesn't have a close on the scanner.  

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.

. . . not catching the exception and instead declare it with a throws clause . . .

That is exactly what the MOOC code is doing; the private readWords() method throws an exception and the calling method constructor handles it.



Got it. Is this what is called "bubbling up" which Knute Snortum mentioned?    

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.


Thank You for the example.  I'll be more specific about the exceptions which will be thrown.  

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.



It is.  The MOOC course was developed back in 2013 I think.    I wasn't sure if it just hadn't gotten to the point where it taught Try with Resources when you first brought it up to me.   Now I see it's just an out of date type of thing.  I'll have to keep that in mind.  I am on week 9 and there are 12 weeks total so I'm almost at the finish line with this course.



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.



Thank you!  In my original solution I used hasNext() because it seemed to me that it would get the next word rather than the entire line.  I didn't think I would want that .  I need to play around and read up about that.  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.


Campbell Ritchie wrote:

. . . why [is] "vowels" . . . a final field? . . . Who is right here?  Me or MOOC ?  Or does it matter?

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.

Thanks for any help.


. . .

That's a pleasure



Ah got it!  Okay .  Originally I was not thinking final when I said make it local so thank you!  I'm still trying to wrap my head around WHEN to make something final .


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:-



Thank you for the Stream examples.  I saw your post about beginners needing to be familiar with Streams and it's something I was looking at .  
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


In this situation, it mean a "chunk" of data, the data between two delimiters.  It could be a line, a word, or even a letter, depending on the delimiter.
 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



It's me not you.  Since I've been trying to learn Java , there are many things which surround Java and programming in general which I often get lost on.  
Thank you for your patience however .
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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? . . .

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.

The whole idea of letting an exception bubble up (I would prefer to say let it propagate) is that you are allowing the application to crash.

The Java™ Tutorials go to lots of effort to tell you how to handle exceptions, but there are two other things you need to know about exceptions:-
  • 1: If something goes wrong, how am I going to signal that?
  • 2: If I suffer an XYZException here, what am I going to do about it?
  • Afraid I haven't got the time to explain any more now; maybe tomorrow.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Lisa Austin wrote:. . . I'm still trying to wrap my head around WHEN to make something final . . . .

    Follow the Winston Gutkowski school of computing. Make everything in sight final, and take the final modifier off if you have any problems.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Lisa Austin wrote:. . . Is this what is called "bubbling up" which Knute Snortum mentioned?

    No, I don't think it is.

    . . . What does the word "token" in the javadoc exactly mean?

    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.

    . . . post about beginners needing to be familiar with Streams . . .

    Well, that is my opinion, but I am completely correct

    Some suggestions about how you might handle exceptions here, as threatened last night.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Yesterday, I wrote: . . . the Winston Gutkowski school of computing. . . .

    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.
     
    Lisa Austin
    Ranch Hand
    Posts: 333
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Lisa Austin wrote:. . . Is this what is called "bubbling up" which Knute Snortum mentioned?

    No, I don't think it is.

    . . . What does the word "token" in the javadoc exactly mean?

    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.

    . . . post about beginners needing to be familiar with Streams . . .

    Well, that is my opinion, but I am completely correct

    Some suggestions about how you might handle exceptions here, as threatened last night.



    Thanks Campbell Ritchie for all your help.  I'm a self-learner so all the extra information is good.  I'll put into practice better exception handling with my java practice and go through all the info you gave.
     
    Lisa Austin
    Ranch Hand
    Posts: 333
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Yesterday, I wrote: . . . the Winston Gutkowski school of computing. . . .

    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.



    Got it.  I think I understand this.  Because if a method is public and something is using it then changing it from public -> private would break things.   But private methods wouldn't have another outside class calling it so making it public wouldn't break anything.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Lisa Austin wrote:. . . Got it.  . . . .

    Yes, you have
     
    Lisa Austin
    Ranch Hand
    Posts: 333
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Lisa Austin wrote:. . . Got it.  . . . .

    Yes, you have



    Sweet!  Thanks again!  I appreciate all of time you take teaching me to do the right things.  
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Knute Snortum wrote:. . . any method called from a constructor should be final.

    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.
     
    Marshal
    Posts: 8856
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Didn't read thread yet, but that's unrelated thing.

    Looking to your class WordInspection whose constructor accepts File argument and get feelings like

    Bear in mind, that such APIs become very unpleasant and confusing to use, because before you even initialize such object, you need to start reading documentation to decrypt why it is so. At least I'd expect such class accept a word, in a String format perhaps.

    Think if you could improve your API and reduce such confusion.

    Imagine if you could use it in such way:


    So once again, public WordInspection(File file) {...} is not intuitive for me why I need to pass File object to constructor.

    Even FileInspection class name in this case would work better. So its use would look like:
     
    Liutauras Vilda
    Marshal
    Posts: 8856
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Names perhaps are one of most important concepts in programming. APIs, programs make sense when you read only when the names don't lie to you. When method(s) are short and do one thing only, so for the reader it is easy(-ier) to track/understand what it is doing.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Lisa Austin wrote:. . . . Is this what is called "bubbling up" which Knute Snortum mentioned? . . .

    I wrote:No.

    To all intents and purposes, Knute wrote:Yes.

    Hahahahahahahahahahahahahahaha!
     
    Knute Snortum
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Go with Campbell, he has the experience.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    . . . even to know better than Knute does what he means? I usually say, “propagate.”
     
    Knute Snortum
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:. . . even to know better than Knute does what he means?


    Especially that!

    I usually say, “propagate.”


    Propagate it is then, since bubble up is often said of events.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Knute Snortum wrote:. . . Propagate it is then . . .

    And what did Salvin and Stephan say in this threa‍d?
     
    Knute Snortum
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well, they need to get in line!  ;)
     
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Lisa,

    your topic is one of the top topics in the month June. So, enjoy a cow!
     
    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
    reply
      Bookmark Topic Watch Topic
    • New Topic