This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!

Lisa Austin

Ranch Hand
+ Follow
since Jun 04, 2015
Cows and Likes
Cows
Total received
4
In last 30 days
1
Total given
0
Likes
Total received
6
Received in last 30 days
0
Total given
67
Given in last 30 days
4
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Lisa Austin

Hey Knute Snortum I hope you see this.      
Can you please tell me if I have the right idea?  Also again my address part isn't finished as of yet still.


Knute Snortum wrote:One of the things I noticed is that you have UI in your Person class.  addPhone() shouldn't prompt for or input a phone number, it should just be:




Okay so I took out ALL user input from my Person class.


Knute Snortum wrote:

Also, I think UserInterface is doing more than it should be.  The actual logic of putting data into Persons and search should be a its own class.  Let's call it PersonMaintenance.  This class would call UserInterface to print the menu and to prompt and get data.  It would contain the logic for searching Persons.  The Search class would go away in this model.  Whether you call UserInterface to print "not found" or "phone not found" would be in PersonMaintenance.  Anything with a System.out.println() or a scanner would be in UserInterface and only there.

Does this structure make sense to you?



I did the following:
  • Removed the Search class
  • I created the PersonMaintenance class


  • On the PersonMaintenance class
  • All of the logic of putting data and searching for data


  • On the UserInterface class
  • Has all print statements ( Anything with System.out.println() )
  • Prints the menu , and prompts for UI



  • Knute Snortum wrote:

    Whether you call UserInterface to print "not found" or "phone not found" would be in PersonMaintenance.



    But yet I don't know how to make this happen .    Right now I have the UserInterface calls methods on PersonMaintenance if the method returns a null value it prints that the phone number wasn't found but I think you are suggesting the opposite?  If the phone number isn't found, the method doing the phone number search calls a methods on the UserInterface to prints the statements?  


    Here are my changes














    2 days ago

    Knute Snortum wrote:One of the things I noticed is that you have UI in your Person class.  addPhone() shouldn't prompt for or input a phone number, it should just be:

    Also, I think UserInterface is doing more than it should be.  The actual logic of putting data into Persons and search should be a its own class.  Let's call it PersonMaintenance.  This class would call UserInterface to print the menu and to prompt and get data.  It would contain the logic for searching Persons.  The Search class would go away in this model.  Whether you call UserInterface to print "not found" or "phone not found" would be in PersonMaintenance.  Anything with a System.out.println() or a scanner would be in UserInterface and only there.

    Does this structure make sense to you?



    Thank You.  I THINK it makes sense to me.  After writing this post I was skimming over class about design and I believe I saw something like what you are talking about.   I'll try and put what you suggest in practice.  Thank You

    2 days ago

    Swastik Dey wrote:

    The searchPhoneNumberByName(Person person, String name ) doesn't return anything.  It only calls a search method on the Person class and prints the result.




    The return type of the method is void.  What are you trying to return from this method?



    This is the method called when the user enters command #2 and currently it prints "  not found" when person.getPhone returns a null or empty.      If the user enters command #5 the method searchForAllInformation(Person person) is called.  In the method it calls searchPhoneNumberByName(Person person, String name) because the assignment requires a person's phone number to be printed as part of command #5 but if person.getPhone returns a null or empty the assignment expects "  phone number not found" to be printed.

    My question is.     Is there a good option for both commands to use the method searchPhoneNumberByName(Person person, String name) and still print the two different messages for the same scenario ( if person.getPhone returns a null or empty the assignment expects " )?    

    I'm not asking a How To question .  Like I'm not asking how to get searchPhoneNumberByName(Person person, String name) to return anything.  I know it's a void right now and I can change it to a string and return those messages rather than have them print out.   That maybe what I need to do.  Have it return the Null and based on whether the user had entered "2" or "5" , it prints the appropriate response.

    I guess I was hoping someone would see how I have things organized / done and could help me see other options than what I see.


    I'm stuck here
    I can't think about what to do past the following options and none seem correct.

    A.  Repeat the phone number search I used for #2 rather than have #5's method, searchForAllInformation call searchPhoneNumberByName.   But that goes against the whole DRY concept doesn't it?
    B.  change searchPhoneNumberByName to return a String and handle printing out the results on  the UserInterface class?  It seems like I should just get rid of the Search class then.
    C.  change searchPhoneNumberByName to call another method on the Search class to handle the null and empty?





    2 days ago
    Introduction
    I'm working through an online course, MOOC Java part 2 https://materiaalit.github.io/2013-oo-programming/part2/week-9/.   Week 9, section 26.  It's the last assignment for this section and we are supposed to design the solution on our own.   That being said, I'm struggling with trying to keep from repeating code within methods.  Also object oriented.  The below program isn't a finished program.  It needs the Address part , and add error handling.

    I hope I can explain this well enough.  The issue really is with two commands at the moment and what to do with responses expected when the information isn't found.  It's not the entire program.  

    The assignment:

    The assignment is a phone and address search.   Right now I'm working through the phone part and the problem I'm struggling with is the  assignment wants slightly different expected results between #2 and #5 when the phone number isn't found.
    The program is supposed to have 8 commands .  1 - 7 does different things, 8 is to quit.

    Commands 1, 2 and 5:
  • 1. adds a (phone) number
  • 2. search(es) for a (phone) number
  • 5. search(es) for personal information ( i.e. phone number and address )


  • Example:



    The Issue:

    Both #2 and #5 searches for a phone number using the person's name.   But if the phone number isn't found, the expected result for #2 is "not found" where #5 it's "phone number not found".

    In the Search class.
    For command #2 I have a method name searchPhoneNumberByName(Person person, String name ).  
    For #5 I have method name searchForAllInformation(Person person) and inside that method it calls the searchPhoneNumberByName(Person person, String name ) method.  

    The searchPhoneNumberByName(Person person, String name ) doesn't return anything.  It only calls a search method on the Person class and prints the result.




    I'm stuck here
    I can't think about what to do past the following options and none seem correct.

    A.  Repeat the phone number search I used for #2 rather than have #5's method, searchForAllInformation call searchPhoneNumberByName.   But that goes against the whole DRY concept doesn't it?
    B.  change searchPhoneNumberByName to return a String and handle printing out the results on  the UserInterface class?  It seems like I should just get rid of the Search class then.
    C.  change searchPhoneNumberByName to call another method on the Search class to handle the null and empty?  


    Help Please

    I'm stuck.   I think I may just need to re-organize everything but I'm not sure what is the best way.    Should I get rid of the Search class altogether and instead do the searching in my UserInterface class ?  This would eliminate one layer at least.  I could have a method to call Person's getPhone method and another method to handle the null responses on UserInterface rather than on Search.




    Program is organized as

    I have 6 class files.
  • Main - just to start the program
  • UserInterface - gives instructions, takes user's commands.
  • Search - performs the various searches
  • Person - stores the phone and address information.  
  • Phone - "helper" class ( I think that's what it's called ).  Just stores the Phone info
  • Address - Same as Phone but for addresses











  • UserInterface class




    Search class




    Person






    Phone



    Address











    Main







    3 days ago

    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.  
    2 weeks ago

    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.
    2 weeks ago

    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.
    2 weeks ago

    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 .
    2 weeks ago

    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 .  
    2 weeks ago

    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.

    2 weeks ago

    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.



    2 weeks ago

    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.  








    2 weeks ago
    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]
    2 weeks ago

    Campbell Ritchie wrote:

    Lisa Austin wrote:. . . Are you referring to when I did  (reader.hasNext()) for example. . . .

    No. This is what you should do:-

    }[/code]As long as you are only using one Thre‍ad, you only need one Scanner per application, so there is no point in creating more instances.




    Ah got it.  Thank You very much.  
    3 weeks ago

    Carey Brown wrote:The advice about not closing a Scanner is specific to one scenario, the case in which you create a new Scanner from System.in.

    Also note that there should only ever be one Scanner created from System.in. Do not open multiple Scanners from System.in nor create them in a loop.



    Okay got it.  
    Regarding the advice about not opening multiple Scanners from System.in or creating them in a loop.  Are you referring to when I did  (reader.hasNext()) for example.  Is that what you are referencing?  
    3 weeks ago