import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console cnsl = null;
String name = null;
try{
// creates a console object
cnsl = System.console();
// if console is not null
if (cnsl != null) {
// read line from the user input
name = cnsl.readLine("Name: ");
// prints
System.out.println("Name entered : " + name);
}
}catch(Exception ex){
// if any error occurs
ex.printStackTrace();
}
}
}
jagvllead 0
Newbie Poster
Recommended Answers
Jump to Postwelcome to daniweb :)
apart from scanner , you can also try out a BufferedReader .
i dont really see console being used for this kind of stuff. any particular reason you're using it ?also , whatever you choose to use , its a good practice to close the …
All 3 Replies
stultuske 1,116
Posting Maven Featured Poster
somjit{} 60
Junior Poster in Training Featured Poster
JamesCherrill 4,733
Most Valuable Poster Team Colleague Featured Poster
<M/>
commented:
:) +8
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.