Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# How to take user input as integers?

Aakash Goel
Ranch Hand
Posts: 198
Suppose I write a program to multiply two numbers.

The user enters the numbers as follows:

2
3

How do I extract these integers inside main?
[please explain the case where the integers are on two separate lines].

What to do for the newline inbetween?

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34974
379
Aakash,
What do you know how to do so far? Do you know how to read a line from standard in? Do you know how to convert a string to an integer?

John Michaels
Greenhorn
Posts: 7
Well... in order to read something from the user, you need to import the scanner. You would type above your class:
Then you would need to initialize the scanner object as so:
"reader" in the above line can be anything you want.
Now Lets pretend you're asking the person to enter the two integers:
After this you will need to read the two integers. You do this with the scanner function nextInt(); like so:

Bear in mind, it's "reader." because we called initialized the scanner object as reader. If it was something else, we would type [something else].nextInt();

No problem man. It's tough being a newbie. ;)

Aakash Goel
Ranch Hand
Posts: 198
Jeanne Boyarsky wrote:Aakash,
What do you know how to do so far? Do you know how to read a line from standard in? Do you know how to convert a string to an integer?

I am preparing for SCJP5 but I am not very well versed with IO.

Do you know how to read a line from standard in?
-->NO

Do you know how to convert a string to an integer?
-->YES

What is the fastest method to read an integer from the standard input?

Aakash Goel
Ranch Hand
Posts: 198
John Michaels wrote:Well... in order to read something from the user, you need to import the scanner. You would type above your class:
Then you would need to initialize the scanner object as so:
"reader" in the above line can be anything you want.
Now Lets pretend you're asking the person to enter the two integers:
After this you will need to read the two integers. You do this with the scanner function nextInt(); like so:

Bear in mind, it's "reader." because we called initialized the scanner object as reader. If it was something else, we would type [something else].nextInt();

No problem man. It's tough being a newbie. ;)

Thanks. It worked. But, is there a faster way [Scanner is efficient but slow ] to do the same?

pete stein
Bartender
Posts: 1561
Aakash Goel wrote:Thanks. It worked. But, is there a faster way [Scanner is efficient but slow ] to do the same?

How do you mean it's slow? It works almost instantaneously on most computers. Is there more to your problem than we know about?

Aakash Goel
Ranch Hand
Posts: 198
pete stein wrote:
Aakash Goel wrote:Thanks. It worked. But, is there a faster way [Scanner is efficient but slow ] to do the same?

How do you mean it's slow? It works almost instantaneously on most computers. Is there more to your problem than we know about?

Yeah, it does work instantaneously.

What I mean to say is that,
When I compared it with a C program I wrote for the same job [just taking inputs], it was comparatively slower [took approximately twice the time].
So, is there a faster way?

pete stein
Bartender
Posts: 1561
Aakash Goel wrote:
Yeah, it does work instantaneously.

great

What I mean to say is that,
When I compared it with a C program I wrote for the same job [just taking inputs], it was comparatively slower [took approximately twice the time].
So, is there a faster way?

1) I know of no faster way, though that doesn't mean it doesn't exist.
2) If we're discussing getting input from a user sitting at a console, this smells of a micro-optimization non-issue since the speed of the Scanner's interactions is measured in milliseconds, while the speed of the user entering data is measured in seconds. Speeding up the scanner even by 50% or more will not change any of the perceived characteristics of the program. Again, I have a feeling that there's more about this issue than you have yet told us.

Bear Bibeault
Author and ninkuma
Marshal
Posts: 65229
95
Aakash Goel wrote:[took approximately twice the time].

So? 2 * n is pretty much the same as n as the limit of n approaches zero.

(and yes, I know this is an old question, but apparently one that comes up in search.)