• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic