Results 1 to 6 of 6
  1. #1
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Proper use of Scanner

    I've been contemplating on the methods in which I use my scanner object and I want people to verify if the way I doing it is considered good practice. A lot of times I have different methods in a class. Each of those methods have local variables that I want to read in with my scanner object. I've heard from people that if you're using more than one scanner object, than you're doing it wrong; This I tend to avoid. Other times, you can pass in scanner as an argument in a method but I've also heard from people that you have to keep track of the state that the scanner is in which I don't really know how to do so I also try to avoid this. The last way I know how is to put the scanner object outside of the method as private static. This is what I usually do but I don't know if this could have harmful effects on my program. So, in conclusion, whats the best way to implement scanner with minimal risk to the program when dealing with multiple methods that have local variables that you want to read in. Thanks for reading

  2. #2
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Proper use of Scanner

    I would also like to ask if it's considered bad practice to also have multiple objects of a class in different methods.

  3. #3
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Proper use of Scanner

    Anybody has anything to say?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Proper use of Scanner

    I will say that if the method(s) that requires these local variables is doing something useful (say a calculation) then I would not be having that method ask the user.
    I would ask beforehand and pass the data to the method.

    Essentially, you want to try and keep your user interaction (the user interface) code separate from the code that does the number crunching.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Proper use of Scanner

    Ok, I understand that but what if the method is just asking for personal information like what's your name or what's your height? How about that type of information. It would also be nice if you answer my question of using multiple class objects in different methods.
    Last edited by Deathslice; 02-06-2015 at 04:08 PM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Proper use of Scanner

    Quote Originally Posted by Deathslice View Post
    Ok, I understand that but what if the method is just asking for personal information like what's your name or what's your height? How about that type of information. It would also be nice if you answer my question of using multiple class objects in different methods.
    As was already stated, it is probably best to try and consolidate your input to as few locations as possible. But that doesn't mean you can't create and use methods to facilitate that process. Something along the lines of:

    Java Code:
    int age = promptInt("Please enter age: ")
    String lastName = promptString("Please enter last name: ");
    The prompt methods could even call a method to validate user input before returning.

    I'm not certain what you mean regarding using multiple objects in different methods. Of course you can use as many as you want or need to get the job done. If you are worried about constantly recreating expensive objects then think about using singletons. If you don't know what a singleton is, search the web.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Proper Pronunciation of GUI?
    By pj6444 in forum New To Java
    Replies: 2
    Last Post: 01-07-2014, 05:26 PM
  2. Proper use of PreparedStatment
    By SnakeDoc in forum JDBC
    Replies: 6
    Last Post: 03-08-2013, 05:08 PM
  3. Proper if Statements: Help
    By socboy6579 in forum New To Java
    Replies: 3
    Last Post: 12-10-2010, 01:48 PM
  4. while and proper conditions for if
    By Saletra in forum New To Java
    Replies: 11
    Last Post: 08-25-2010, 10:37 AM
  5. proper use of IllegalArgumentException
    By vendetta in forum New To Java
    Replies: 1
    Last Post: 01-16-2010, 07:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •