wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple input program....or, not so simple?" Watch "simple input program....or, not so simple?" New topic
Author

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

Lucas Mac
Greenhorn

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)..here is the code i wrote:
==================================================================

import java.io.*;

class SimpleInput {
public static void main(String[] args) {
int value;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
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) {
System.err.println(ioe.getMessage());
}
}
}
===============================================================
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


yup
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: http://aspose.com/file-tools
 
subject: simple input program....or, not so simple?