• Post Reply Bookmark Topic Watch Topic
  • New Topic

Input statements in Java?  RSS feed

 
Varalakshmi Ramanarayan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For data input we used to have scanf in C, and cin in C++. But I dont see anything like that in Java.
(Currently i'm going through Paul Sangheras book for SCJP5)
What should I do If I have to input some data during execution?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as with all software decisions, it depends.

you can build a GUI and used the widgets (text boxes, drop downs, radio buttons).

you can pass arguments to your program when you start it.

you can read a file.

you can have the user type something on the command line.

So, which way would you like to do it?
 
Varalakshmi Ramanarayan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple example, In C++ we used to do like this,


How can I do the same thing in java?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To read from the keyboard, and to write to the terminal, the easiest method is to go through the System.in and System.out instances.

Henry
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JDK 6 they added java.io.Console, which is probably a bit easier to use than System.in.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at class java.util.Scanner.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!