• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to read value from console ??

 
Oni Anand
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i want to read some value from console..could someone pls gimme one example ??
tx
Oni
 
jayakeshpk
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
assuming that you want to read input from the dos prompt.
You can use the BufferedReader class in java.io
Here is a small peace of code which may help.
This is to read one line from the input.
BufferedReader br = new BuferedReader(new InputStreamReader (System.in));
System.out.println("Enter name");
String name=br.readLine();
This may help
Thanks
 
Oni Anand
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks lot
 
Tom Hennigan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//This application might help you.
// Java code to read console input from a Microsoft DOS command line
import java.io.*;
public class ReadConsole {
public static void main(String args[]) throws IOException {
// declare your InputStream and OutputStream objects
InputStream inStream;
OutputStream outStream;
int inChar;
// assign System.in to inStream and System.out to outStream
inStream = System.in;
outStream = System.out;
System.out.println("Reads input from console ... Ctrl+Z to quit.");
System.out.println("After Ctrl+Z, writes console input on monitor .... ");
// read (that is, execute the loop)
// from console until end-of-file (Ctrl+Z)
try {
while ( ( inChar = inStream.read() ) != -1 ) {
outStream.write( inChar );
}
}
// catch block to handle IOException:
catch ( IOException ioe ) {
System.out.println( "Error: " + ioe.getMessage() );
}
// ensure that streams are closed
// irregardless of whether an IOException occurs
finally {
inStream.close();
outStream.close();
}
}
}
 
Don Smathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to explore JOptionPane in javax.swing.*
Also, David Eck at hws.edu provides a file called TextIO that is an excellent set of both input and output routines. Everyone I've used works great.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!