*
The moose likes Beginning Java and the fly likes How to take user input as integers? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to take user input as integers?" Watch "How to take user input as integers?" New topic
Author

How to take user input as integers?

Aakash Goel
Ranch Hand

Joined: May 26, 2008
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?


SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30598
    
154

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?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
John Michaels
Greenhorn

Joined: Jun 01, 2009
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

Joined: May 26, 2008
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?


sorry for the late reply.

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

Joined: May 26, 2008
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

Joined: Feb 23, 2007
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

Joined: May 26, 2008
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

Joined: Feb 23, 2007
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

Joined: Jan 10, 2002
Posts: 61318
    
  66

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.)


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to take user input as integers?