Three basic methods for passing parameters to the system in Java

Source: Internet
Author: User

How to pass parameters using the Main method

The example code is as follows:

public class MainArgs {public static void main(String[] args) {System.out.println(args.length);for(String str : args){System.out.println(str);}}}


Jvm will assign one or more strings to The args array after running the java program (one or more strings are separated by spaces. When a string contains spaces, you must enclose the complete string. Example:

Use the metric class for user input: You can enter the data type specified by the user

The separator mode is used to separate the input into tags. By default, the separator mode matches the blank. Then you can use differentNextMethod to convert the resulting tag to different types of values.

The example code is as follows:

Import java. util. imports; import java. io. file; public class ScannerKeyBoardTest {public static void main (String [] args) throws Exception {// readFileCon (); // test2 (); // enter the specified category scan = new category (System. in); Long l = scan. nextLong (); System. out. println ("l is" + l);} // returns Stringpublic static void test1 () {shortscan = new partition (System. in); // use the carriage return key as the Separator by default, and use the space tabulation key and press enter as the delimiter to pay separately. // Scan. useDelimiter ("\ n"); while (scan. hasNext () {System. out. println ("next is" + scan. next () ;}/// Longpublic static void test2 () {shortscan = new feature (System. in); // when the input is not a Long value, the loop while (scan. hasNextLong () {// blocking // System. out. println ("has over scan. nextLong () begin .... "); System. out. println ("next is" + scan. nextLong (); // System. out. println ("scan. nextLong () over has begin .... ") ;}} // read the content in the File and print it to the public static void readFileCon () throws Exception {scan = new topology (new File (" ScannerKeyBoardTest. java "); System. out. println ("fileContent is:"); while (scan. hasNextLine () {System. out. println (scan. nextLine ());}}}
Read user input using the BufferedReader class: only the String class is returned.

The example code is as follows:

Import java. io. bufferedReader; import java. io. inputStreamReader; class BufferReaderKeyBoardTest {public static void main (String [] args) throws Exception {BufferedReader br = new BufferedReader (new InputStreamReader (System. in); String in = null; while (in = br. readLine ())! = Null) {System. out. println ("user input:" + in );}}}

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.