• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accepting data in a simple program.

 
Rahul Dasgupta
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am new to java and was trying to write a program to add two numbers which the user provides at runtime, but I cant find the command to accept data from the user.
Can some one help me?
Thanks,
- Rahul
 
Brian Glodde
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rahul,
Below is a simple command-line program that will accept 2 inputs, then attempts to add them. If the input is non-numeric, a NumberFormatException is thrown. Hope it helps to clear up the concept for you.

[ June 01, 2002: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and here is an example program that prompts the user for data:

Good Luck.
 
Rahul Dasgupta
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Dick,
I tried out the code given below, and it has worked. This was mostly from your second code example ( because I wanted a program that prompts the user for data, and this could be a base for later programs which would be based upon interaction with the user). But the code seems a bit too long for such a simple program, and I am not very well versed yet with streams or exceptions.
What I was really looking for was the java equivalent of the C++ command :
cin<< variable ;
Anyway, thanks a lot for your help.
- Rahul
P.S. What does SCJP2 & SCWCD stand for? (I can guess that SCJP stands for Sun Certified Java Programmer but what is the 2 for?)


[ June 01, 2002: Message edited by: Rahul Dasgupta ]
[ June 02, 2002: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Dasgupta:
Hello Dick,

Sure, call me names, then have the nerve to ask for more advice! I've never...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SCJP2 more or less stands for Sun Certified Programmer for the Java 2 Platform. Some may argue that it actually stands for Sun Certified Java Professional, but I think Sun more frequently uses the previous phrase.
SCWCD stands for Sun Certified Web Component Developer.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...the code seems a bit too long for such a simple program...
The code does indeed not need to be quite so long. Notice that a few parts are more or less exactly repeated. This repetition isn't necessary.
One hint for simplification: Do you really need two BufferedReaders? Or could you just reuse the original one? In that vein, is it possible to reuse other parts of the code as well?
Getting any ideas?
 
Rahul Dasgupta
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to have misspelled your name, Dirk. It seems to have really upset you.
- Rahul
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody's upset on this end. Just fun and games...
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic