• 0

[JAVA] Using Scanner


Question

Hey all,

Got a quick question about using the scanner class - which I'm not too familiar with.

Let's say I'm making a command line interface, something really simple like press 1 for this, 2 for that.

Now I have a method called menu() that creates a new Scanner (Scanner in = new Scanner(System.in);). It then grabs the int from there, and depending on the number, calls another method.

My problem is, in my other methods where input is required... how do I "enable" it again. It's not letting me input anything apart from the first time.

Do I need to create another new Scanner for that method, or can I use the same one system wide?

Help please :D:D

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

you could use the same one, if it is passed by reference or is a global variable.

But what is wrong with just declaring another scanner?

(Dont forget to close the first scanner when your done with it!!)

I haven't touched java in a few years, but according to this website

all you need to do is to do something like in.nextLine()/nextDouble() etc to grab the next line from console.

Link to comment
Share on other sites

  • 0

Since you are using integers you could simply go

int variablename = in.nextInt();

return variablename;

EDIT: if you have a separate function to take the input the Scanner would have to be a global variable.

Link to comment
Share on other sites

  • 0
you could use the same one, if it is passed by reference or is a global variable.

But what is wrong with just declaring another scanner?

(Dont forget to close the first scanner when your done with it!!)

I haven't touched java in a few years, but according to this website

all you need to do is to do something like in.nextLine()/nextDouble() etc to grab the next line from console.

Nothing is wrong with declaring another scanner - I was just thinking that wasn't the best way to code it possibly for some reason?

I'll try passing it by reference (i.e. declaring it in one method - and then passing that variable to the other method), something that I was trying to do but for some reason it wasn't working - and wasn't letting me input anything in the new method :s

Link to comment
Share on other sites

  • 0
Nothing is wrong with declaring another scanner - I was just thinking that wasn't the best way to code it possibly for some reason?

I'll try passing it by reference (i.e. declaring it in one method - and then passing that variable to the other method), something that I was trying to do but for some reason it wasn't working - and wasn't letting me input anything in the new method :s

Ignore me... totally.

I've just realised all along, in my IF instead of calling the method (that then asked for additional input)... I simply had a System.out.println() saying the same thing.

Fixed it now and it works perfectly. BLEH!

Cheers guys!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.