• Post Reply Bookmark Topic Watch Topic
  • New Topic

One scanner to rule them all  RSS feed

 
Christian Caudill
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
​Is there any way to use scanner to get input and distinguish between int and text? I'm trying to make a scanner method I can call whenever I need user input no matter if I need int or text. Using two methods (one for int, other for text) at the moment.


 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Christian Caudill wrote:?Is there any way to use scanner to get input and distinguish between int and text?

Yes that's what the nextLine(), nextInt() etc methods are for.

Christian Caudill wrote:
I'm trying to make a scanner method I can call whenever I need user input no matter if I need int or text. Using two methods (one for int, other for text) at the moment.

If you want to return different types then you need to ask for the type you want. If you want to try to automate it (which I can't say I'd recommend) you could use hasNextInt() to see if there is an int and if so return it else return a String. The problem with doing this type of thing though is if a sentence happens to begin with a number your method will just return the integer. The next problem is you have to return Object as the code may be returning a String or an Integer so the calling code will have to check which type is being returned.
All in all not a very good solution.

The real question here is not how do you do this but why do you want to do this?
 
Christian Caudill
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the clarity. I was just playing around with Scanner trying to figure out the different ways to manipulate user input. The topic isn't being applied to a specific situation. Mainly I'm just curious of the interactions of requesting user input and passing that to methods such as setAge() and setName(). getUserInputText() and getUserInputInt() are my own code and I wanted to see if they could be combined into a single getUserInput().
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your code is going to call, for example, setAge(myAge) then it knows it needs an int and so can call getUserInputInt() to get the input as an int equally if you want a handle a name the code knows it needs a String so can call getUserInputTest(). This far simpler to implement and much more obvious to anyone else reading the code than trying to use an automated approach.

BTW it's not very efficient to use static methods as you have done to access the Scanner object as you are creating a new Scanner object for every call to the methods.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a Scanner will distinguish numbers from other text. One way to use that is like this:-And (again) welcome to the Ranch
 
Christian Caudill
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank both of you. I'm learning on my own and it's nice to have the help.

@Tony,

How does creating objects for each call effect the memory usage and should I create the object first and just call the methods within Scanner?

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Caudill wrote:How does creating objects for each call effect the memory usage

It won't adversely effect memory usage because once the object is no longer in scope it is eligible for garbage collection. But it does mean that you will get the overhead of creating the scanner every time and also the garbage collector will have more work to do. This isn't going to be at all significant of you only make a couple of calls to the get input but it's not good practice.

Christian Caudill wrote:and should I create the object first and just call the methods within Scanner?

Yes create the Scanner object once and use it multiple times.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!