Three methods for obtaining input from a Java character Terminal

Source: Internet
Author: User

There are three methods to obtain input on the Java character terminal: 1. java. lang. system. in (currently supported by JDK) 2. java. util. (JDK version> = 1.5) 3. java. io. console (JDK version> = 1.6), feature: whether to display the password again refer to: several methods to record information read from the Console in Java (1) JDK 1.4 (JDK 1.5 and JDK 1.6 are also compatible with this method) public class TestConsole1 {public static void main (String [] args) {String str = readDataFromConsole ("Please input string :); system. out. println ("The information from console: + str);}/*** Use InputStreamRea Der and System. in to read data from console ** @ param prompt ** @ return input string */private static String readDataFromConsole (String prompt) {BufferedReader br = new BufferedReader (new InputStreamReader (System. in); String str = null; try {System. out. print (prompt); str = br. readLine ();} catch (IOException e) {e. printStackTrace ();} return str ;}} (2) JDK 1.5 (read using iterator) public class T EstConsole2 {public static void main (String [] args) {String str = readDataFromConsole ("Please input string:"); System. out. println ("The information from console:" + str);}/*** Use java. util. failed to read data from console ** @ param prompt ** @ return input string */private static String readDataFromConsole (String prompt) {repeated bytes = new bytes (System. in); System. out. print (prompt ); Return response. nextLine () ;}} Finally, you can also easily scan files, read the information in it, and convert it to the type you want, for example, to sum data like "2 2.2 3.3 3.33 4.5 done", see the following code: public class TestConsole4 {public static void main (String [] args) throws IOException {FileWriter fw = new FileWriter ("num.txt"); fw. write ("2 2.2 3.3 3.33 4.5 done"); fw. close (); System. out. println ("Sum is" + scanFileForSum ("num.txt");} public static double scanFileForSum (String fileNam E) throws IOException {double sum = 0.0; FileReader fr = null; try {fr = new FileReader (fileName); partition identifier = new partition (fr); while (partial. hasNext () {if (then. hasNextDouble () {sum = sum + sums. nextDouble ();} else {String str = response. next (); if (str. equals ("done") {break;} else {throw new RuntimeException ("File Format is wrong! ") ;}}} Catch (FileNotFoundException e) {throw new RuntimeException (" File "+ fileName +" not found! ");} Finally {if (fr! = Null) fr. close ();} return sum;} (3) JDK 1.6 (using java. io. (java. io. the Console class is dedicated to accessing character-based Console devices. if your program needs to interact with cmd in Windows or Terminal in Linux, you can use the Console class. (similar to System. in and System. but we cannot always get the available Console. Whether a JVM has a available Console depends on the underlying platform and how the JVM is called. if the JVM is started in interactive command lines (such as Windows cmd) and the input and output are not redirected to another place, an available Console instance can be obtained. When IDE is used, the Console instance cannot be obtained because the standard input and output streams are redirected in the IDE environment, that is, redirect the input and output from the system console to the public class TestConsole3 {public static void main (String [] args) {String str = readDataFromConsole ("Please input string: "); System. out. println ("The information from console:" + str);}/*** Use java. io. console to read data from console ** @ param prompt ** @ return input string */private static String re AdDataFromConsole (String prompt) {Console console = System. console (); if (console = null) {throw new IllegalStateException ("Console is not available! ");} Return console. readLine (prompt);} another feature of the Console class is that it processes security characters such as passwords (no echo input. The readPassword () method is provided specially. For specific applications, see the following code: public class TestConsole5 {public static void main (String [] args) {Console console = System. console (); if (console = null) {throw new IllegalStateException ("Console is not available! ");} While (true) {String username = console. readLine ("Username:"); char [] password = console. readPassword ("Password:"); if (username. equals ("Chris") & String. valueOf (password ). equals ("GoHead") {console. printf ("Welcome to Java Application % 1 $ s. \ n ", username); // after use, the array should be cleared immediately to reduce the time it occupies in the memory. Enhanced Security password = null; System. exit (-1);} else {console. printf ("Invalid username or password. \ n ");}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.