aspose file tools*
The moose likes Beginning Java and the fly likes Accepting data in a simple program. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accepting data in a simple program." Watch "Accepting data in a simple program." New topic
Author

Accepting data in a simple program.

Rahul Dasgupta
Greenhorn

Joined: Jun 01, 2002
Posts: 20
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

Joined: Jun 27, 2001
Posts: 171
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

Joined: Dec 10, 2001
Posts: 7023
...and here is an example program that prompts the user for data:

Good Luck.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Rahul Dasgupta
Greenhorn

Joined: Jun 01, 2002
Posts: 20
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

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Dec 10, 2001
Posts: 7023
...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

Joined: Jun 01, 2002
Posts: 20
Sorry to have misspelled your name, Dirk. It seems to have really upset you.
- Rahul
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Nobody's upset on this end. Just fun and games...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accepting data in a simple program.