• Post Reply Bookmark Topic Watch Topic
  • New Topic

Should a scanner only be used in one class? main method?  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

should a scanner only be instantiated in one class?
if i have two classes
car
racetrack

and i wanted to get a user to put in his name via a method in the racetrack class
but i also wanted to get the user to give data in the racetrack class,how would i go about doing this?

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
second racetrack should be car...sorry
 
Wesley Grove
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no issue with using a scanner in two different classes. Scanner is just a class like Math, Car, and Racetrack, and can be used anywhere. If you really wanted to, they could even use clone of the same scanner object, by sending it to the Car (I assume as I don't think that racetrack is having objects made of it and it is the main) object in the constructor. Why you would is beyond me as it is a single line of code to just make a basically identical Scanner object, so doing so would just be more work, but, to each their own.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:and i wanted to get a user to put in his name via a method in the racetrack class


Why would you want to do that? I've seen racetracks, probably you have too, and if you asked the people who go to racetracks about what a racetrack was for, they surely wouldn't say that it was for asking somebody to put in their name.
 
Wesley Grove
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something I've done in the past that should help is to have an input class, which I have used in multiple projects as it already had a majority of the code I would need for any input I might need, and I could add methods if I didn't have a useful one ready to go.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it would be a good idea to create an inputs class. You can then say KeyboardInputs.nextInt("Please enter how many cars: "); and get the number directly. Make the class a utility class with all static members and a private static field for the Scanner. Give it a private constructor because you never need any instances of it. Keep the inputs class for future reference because you are bound to need it again.
Don't try creating multiple Scanners, because that is a waste of time. Use one Scanner object for the whole of your application. Much easier like that. Don't try cloning Scanners; in fact since Scanner doesn't implement Cloneable, you can't. Any attempt to clone it will cause an Exception to be thrown.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
most helpful thanks!!!
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi paul,

Why would you want to do that? I've seen racetracks, probably you have too, and if you asked the people who go to racetracks about what a racetrack was for, they surely wouldn't say that it was for asking somebody to put in their name.

it was just an example
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:most helpful thanks!!!
That's a pleasure
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey ritchie i will build that tonight and post it up and you guys can correct me....
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure on utility class...but i will google and watch videos...
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search my posts on this forum and “Java in General” for utility class. You will probably get the entire keyboard inputs class, but like Lego, in small pieces requiring assembly.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is that how input is usually handled in all commercial/industry standard java applications?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most commerical Java® applications are probably the middle part of the three‑tier architecture which does not use much IO at all.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am i on the right path here...



the utility class should be final?
and the default constructor should be private?.....though i dont understand how/why i need to implement it.

though this way...i cant seem to call the methods from another class...or instantiate it either.

thanks for reading...

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Utility classes usually have only static members, and none of the fields changes. So they are regarded as constants if they are exposed at all. There is therefore no need to create any instances, which explains why you have a single private constructor and never call it. We have spent lots of time telling you that you need one Scanner instance, so why have you created an instance as a local variable in a method?
You are intending to create a utility class for general use, so all your methods shou‍ld be for general use. Why are you calling a method getTeamName? That sounds like an instancee method in a Team class. If you are getting something with KeyboardInputs.methodName(), you need to consider these two things (at least):-
  • 1: What the method is to be called.
  • 2: Which parameters you are going to demand.
  • As for No 2: what are you going to use a String[] for?

    A class with only private constructors is de facto final. I shall leave you to work out why.
     
    jon ninpoja
    Ranch Hand
    Posts: 291
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi guys,

    after reading your posts and watching a few videos i came up with this...

    does this look better?

     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A lot better. But you can refine it much more. You can ensure that nextInt() will return an int and not throw an Exception by using a technique Rob Spoor taught me a long time ago. Search my posts for Scanner hasNextInt and nextInt and you might find it.
     
    jon ninpoja
    Ranch Hand
    Posts: 291
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks campbell your a great help...kudos!!!
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Did you really write
    return getNumber();
    That will go into infinite recursion. You simply want
    return userInput.nextInt();
     
    jon ninpoja
    Ranch Hand
    Posts: 291
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sorry campbell im not with you...can you please explain a little further regarding the return of the variable not nextInt

    also...i was looking up your threads but couldnt find the specific one you mentioned...long story short

    are there methods in the Scanner class that deal if incorrect input...or am i looking at catch try?

    thanks...
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I found 37 threads in this forum alone when I did such a search.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!