Java Base Class Library

Source: Internet
Author: User

1 The Main method runs the parameters of the Java program:The following is a detailed explanation of why the main method uses this method to sign the 1.public modifier: the Java class is called by the JVM and, in order for the JVM to be free to invoke the main () method, exposes the method using the public modifier. 2.static modifier: When the JVM invokes this main method, it does not first create the object of the main class, and then invokes the main method through the object. The JVM directly passes through the class master method, so the main method is decorated with static. 3.void return value: Because the Main method is called by the JVM, the return value of the method is returned to the JVM, which makes no sense, so the main method does not return a value of public class argstest{public static void Main (string[] Ar        GS) {//output args array length System.out.println (args.length);            Iterates through each element of the args array for (String Arg:args) {System.out.println (ARG); }    }} 2. Use scanner to get keyboard input    Incoming parameters when running a Java program can only set a few fixed parameters before the program starts running. For more complex situations, the program needs to get input during the run. Using the scanner class makes it easy to get the user's keyboard input, scanner is a regular expression based text scanner that resolves basic type values and string values from a file, input stream, and string. The scanner class provides multiple constructors, different constructors can receive files, input streams, and strings as data sources for parsing data from files, input streams, and strings. public class scannerkeyboardtest{    public static void main (string[] args)     {      & nbsp System.in represents standard input, which is keyboard input         Scanner sc=new Scanner (system.in);       //Add below A line will have the carriage return as delimiter        //sc.usedelimiter ("\ n");       //Determine if there is a next entry         while (Sc.hasnext ())         {           //Output input   &NBS P         SYSTEM.OUT.PRINTLN ("Keyboard input content:" +sc.next ());       }   }} Scanner mainly provides two methods to scan the input  1.hasnextxxx (): whether there is a next entry, where xxx can be a string representing the basic data type, such as Int,long, if only to determine whether to include the next string, Use Hasnext (). 2.NEXTXXX () to get the next entry. The meaning of XXX is the same as the one in the previous method, by default, scanner uses blank &NBsp; 

Import Java.util.Scanner;
Import Java.io.File;
public class Scannerkeyboardtest
{
public static void Main (string[] args) throws Exception
{

SYSTEM.OUT.PRINTLN ("****************** input is a long type ******************");
Scanner sc0=new Scanner (system.in);
while (Sc0.hasnextlong ())
{

SYSTEM.OUT.PRINTLN ("Input content is:" +sc0.nextlong ());
}
SYSTEM.OUT.PRINTLN ("****************** input is a long type ******************");


System.out.println ("The content entered by ****************** is file type ******************");
Scanner sc1=new Scanner (New File ("Printer.java"));
while (Sc1.hasnextline ())
{

SYSTEM.OUT.PRINTLN ("Input content is:" +sc1.nextline ());
}
System.out.println ("The content entered by ****************** is file type ******************");



System.out.println ("The content entered by ****************** is String type ******************");
System.in represents standard input, which is keyboard input
Scanner sc=new Scanner (system.in);
Add a carriage return as a delimiter in the following row
Sc.usedelimiter ("\ n");
Determine if there is a next entry
while (Sc.hasnext ())
{
Output input Items
SYSTEM.OUT.PRINTLN ("Keyboard input content is:" +sc.next ());
}

System.out.println ("The content entered by ****************** is String type ******************");

}
}

Scanner not only can read the user's keyboard input, but also can read the file input, as long as the scanner object is created by passing in a file object as a parameter, you can let scanner read the contents of the file

Java Base Class Library

Related Article

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.