Java class library and user interaction, java class library Interaction

Source: Internet
Author: User

Java class library and user interaction, java class library Interaction
6.1.1 parameters for running Java programs

Java program entry: main () method signature:

public static void main(String []args)
  • Public: to allow java classes to freely call this main () method by JVM, use the public modifier to expose this method.
  • Static: When JVM calls this main method, it does not create the object of this main class first, and then calls this main method through the object. JVM directly calls the main method through this class.
  • Void: because the main method is called by JVM, the return value of this method will return JVM, which makes no sense.
    Method call rules: who calls the method and who is responsible for assigning values to the form parameters.
    Arstest. java
package code;public class ArgsTest{    public static void main(String [] args){        System.out.println(args.length);        for(String arg :args){            System.out.println(arg);        }    }}

0
If you use the following command to run the above program,

I:\>java code.ArgsTest java String

If one or more strings (separated by spaces) are followed by the class name when running a Java program, JVM assigns these strings to The args array element in sequence.

I:\>java code.ArgsTest "java String"
6.1.2 use keyboard to get keyboard input

The Scanner can be used to obtain the user's keyboard input. The Scanner is a text Scanner Based on regular expressions. It can explain the basic type values and string values from files, input streams, and strings.

  • HasNextXxx (): whether there is another input item. Xxx can be an Int, Long, or other string representing the basic data type. If you only want to determine whether to include the next string, use hasNext ();
  • NextXxx (): Get the next Input
    By default, the delimiter uses spaces (including spaces, Tab spaces, and carriage return) as the delimiter between multiple input items.
Package code; import java. util. *; public class ScannerKeyBoardTest {public static void main (String [] args) {role identifier = new role (System. in); then. useDelimiter ("\ n"); while (random. hasNext () System. out. println ("the content entered by the keyboard is:" + keyboard. next ());}}

The shard read operation may be blocked to wait for the input of information. If the input source is not over and the shard cannot read more input items, both methods will be blocked.
Use useDelimiter (String pattern) to set the delimiter for delimiter.

Package code; import java. util. *; public class ScannerLongTest {public static void main (String [] args) {empty outer = new outer (System. in); while (partial. hasNextLong () System. out. println ("the content entered by the keyboard is:" + keyboard. nextLong ());}}

The above program is not as adaptable as the ScannerKeyBoardTest program, because the ScannerLongTest program requires that the keyboard input must be an integer, otherwise the program will exit
In addition, tokens can be input from files,

Package code; import java. io. *; import java. util. *; public class ScannerFileTest {public static void main (String [] args) throws Exception {writable partition = new partition (new File ("ScannerFileTest. java "); System. out. println ("file content as follows"); while (contents. hasNextLine () {System. out. println (bytes. nextLine ());}}}

The file content is as follows:
Package code;
Import java. io .*;
Import java. util .*;
Public class ScannerFileTest {
Public static void main (String [] args) throws Exception {
Required bytes = new bytes (new File ("ScannerFileTest. java "));
System. out. println ("the file content is as follows ");
While (iterator. hasNextLine ()){
System. out. println (response. nextLine ());
}
}
}

The above program involves file input, which may cause file IO exceptions. Therefore, the declaration of throws Exception indicates that the main method does not handle any exceptions.

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.