File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Lucas Mac
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
chi Lin
Ranch Hand
Posts: 348
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

...
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic