Java-----Scanner Class __java

Source: Internet
Author: User
Package day12_01;
*
 * * Guide package; Create object; Invoke Method
 * There is a static field under the System class:
 * Public     static final inputstream in; standard input stream, corresponding to keyboard input
 *    
 * InputStream is=system.in
 *    
 class demo{
 * public      static final int x=10;
 * Public      static final Student s=new Student ();
 * *      } *  int y=demo.x;
 *  Student S=demo.s;
 * * *  Construction Method:
 *  Scanner (inputstream source)
 /
Import java.util.scanner;//Guide Bag Public
class Scannerdemo {public
       static void Main (string[] args) {
        //Create object
       Scanner sc=new Scanner ( system.in);
       int X=sc.nextint ();
       System.out.println ("x:" +x);
       }



Member Methods

Package day12_02;
Import Java.util.Scanner;
*
 * Basic format:
 * public boolean hasnextxxx ():
 * public Xxx nextxxx ():
 *
 * Example: int
 * Public Boolean has Nextint ()
 * public int nextint ()
 *
 Note
 * inputmismatchexception: input and what you want does not match
* * Public Class Scannerdemo {public
       static void Main (string[] args) {
             //Create object
             Scanner sc = new Scanner (system.in); 
  //Get Data
             if (Sc.hasnextint ()) {
                int x=sc.nextint ();
                System.out.println ("x:" +x);

             }
             else {
                    System.out.println ("The data you entered is incorrect");}}

Two commonly used methods

Package day12_03;
Import Java.util.Scanner; * * Common two methods * public int nextint () * Public String nextline () * * Problem: Get a value first, then get a string * main reason: newline symbol \ n * Solve the problem: * A To get a numeric value, then create a new keyboard input object to get the string * B. All the data is followed by the string, then what, the corresponding conversion of what * * public class Scannerdemo {public static void M
                    Ain (string[] args) {Scanner sc=new Scanner (system.in);
Gets the value//int a=sc.nextint () of two int types;
int B=sc.nextint ();
System.out.println ("A:" +a+ ", B:" +b);

                    System.out.println ("-------------");
Gets the value//String S1=sc.nextline () of two string classes;
String S2=sc.nextline ();
System.out.println ("S1:" +s1+ ", S2:" +s2);

                    System.out.println ("-------------");
Gets a string S1=sc.nextline () in the fetch of an int value//string;
int B=sc.nextint (); System.out.println("S1:" +s1+ ", B:" +b);

                    System.out.println ("------------");
First get an int value, in get a string//int a =sc.nextint ();
String S2=sc.nextline ();

                    System.out.println ("A:" +a+ ", S2:" +s2);

                    int A=sc.nextint ();
                    Scanner sc2=new Scanner (system.in);
                    String S=sc2.nextline ();
             System.out.println ("A:" +a+ ", S:" +s);
 }
}

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.