• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to take user input as integers?

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Yup, yup, yup. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic