File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes simple input program....or, not so simple? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple input program....or, not so simple?" Watch "simple input program....or, not so simple?" New topic

simple input program....or, not so simple?

Lucas Mac

Joined: Jun 12, 2004
Posts: 10
i'm very experienced with c/c++ but just recently started learning java for a project i'm doing a school...i decided to write a simple program where the user just inputs something and it gets printed out again...this is VERY simple in c++, however, i was suprised that it wasn't so in java (since java is supposed to be so much simpler) is the code i wrote:


class SimpleInput {
public static void main(String[] args) {
int value;
BufferedReader stdin = new BufferedReader(new InputStreamReader(;
String input;

System.out.print("Enter a number: ");
try {
input = stdin.readLine();
value = Integer.parseInt(input);
System.out.println("You entered: " + value + ".");
catch(IOException ioe) {
first of all, why do i need all these silly exception handling nonsense? I JUST WANT SOME SIMPLE INPUT. also, what's with all the other complicated stuff just to do what c++ simply does in a couple lines using cin/cout. i read on many sites that this is the best way to write this type of functionality in a program..i just want to know why it has to be so complicated

chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348

user just inputs something and it gets printed out again

Things got complicated because you are not just got
anything from user then output it.

With System.out.print("Enter a number: ");
You are asking user to enter a number, then Integer.parseInt(input)
convert the input from String to integer.

Problem is user may not always follow the instruction and might enter something that is NOT parsable to integer.

To check this, the code was wrapped into try-catch block for possible Exception.
[ June 13, 2004: Message edited by: chi Lin ]

not so smart guy still curious to learn new stuff every now and then
I agree. Here's the link:
subject: simple input program....or, not so simple?
It's not a secret anymore!