Java class libraries interact with users

Source: Internet
Author: User

6.1.1 parameters for running Java programs

Java Program Entry: The method signature of the main () method:

publicstaticvoidmain(String []args)
    • Public: In order for the Java class to be free to invoke this main () method by the JVM, use the public modifier to expose this method.
    • STATIC:JVM call this main method, the object of the main class is not created first, then the main method is invoked through the object, and the JVM directly invokes the main method through the class.
    • VOID: Because the Main method is called by the JVM, the return value of the method returns to the JVM, which does not make any sense.
      Rules for method invocation: Who invokes the method, and who is responsible for assigning the parameter value.
      Argstest.java
package code;publicclass ArgsTest{    publicstaticvoidmain(String [] args){        System.out.println(args.length);        for(String arg :args){            System.out.println(arg);        }    }}

0
If you want to run the above program instead of the following command,

IString

If you run a Java program with one or more strings (separated by spaces) immediately after the class name, the JVM assigns the strings to the args array element in turn.

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

Using scanner, you can get the user's keyboard input, scanner is a regular expression based text scanner, which can be interpreted from the file, input stream, the string to interpret the basic type values and string values.

    • Hasnextxxx (): whether there is a next input, XXX can be int, long, etc. representing the basic data type of the string, if only to determine whether to include the next string, the direct use of hasnext ();
    • Nextxxx (): Gets the next entry
      By default, scanner uses whitespace (including spaces, tab blanks, carriage returns) as delimiters between multiple entries.
package code;import java.util.*;publicclass ScannerKeyBoardTest{    publicstaticvoidmain(String []args){        new Scanner(System.in);        scanner.useDelimiter("\n");        while(scanner.hasNext())            System.out.println("键盘输入的内容是:" + scanner.next());    }   }

Scanner read operations may be blocked to wait for information input, if the input source does not end, scanner can not read more input items, two methods will block
Set the delimiter for scanner using the Usedelimiter (String pattern) method

package code;import java.util.*;publicclass ScannerLongTest{    publicstaticvoidmain(String [] args){        new Scanner(System.in);        while(scanner.hasNextLong())            System.out.println("键盘输入的内容是:" +scanner.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, or the program will exit
And scanner can also be entered from the file,

package  code;import  java.io.*; import  java.util.*; public  class  scannerfiletest  { public  static  void  main  (String []args) throws  exception{Scanner Scanner = new  Scanner (new
      File ( "Scannerfiletest.java" ));        System.out.println ( "file contents as follows" ); while  (Scanner.hasnextline ())        {System.out.println (Scanner.nextline ()); }    }}

The file contents are as follows
Package code;
Import java.io.*;
Import java.util.*;
public class scannerfiletest{
public static void Main (String []args) throws exception{
Scanner Scanner = new Scanner (New File ("Scannerfiletest.java"));
System.out.println ("Document contents as follows");
while (Scanner.hasnextline ()) {
System.out.println (Scanner.nextline ());
}
}
}

The above program involves file input and may cause file IO exceptions, so declaring throws exception indicates that the main method does not handle any exceptions.

Java class libraries interact with users

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.