Re-stepping on Java Road _day12 (scanner,string)

Source: Internet
Author: User

Use of 1:scanner (learn)
(1) The class that appears after JDK5 for keyboard input data.
(2) Construction method:
A: Explain this thing.
It is actually the standard input stream, which corresponds to the keyboard entry, in is the final static variable in the System class: public static final InputStream; In the "standard" input stream. This stream is open and ready to provide input data. Typically, this stream corresponds to keyboard input or another input source specified by the host environment or user.
B: Construction method
InputStream is =;

Scanner (InputStream is)
C: Common formats
Scanner sc = new Scanner (;
(3) Basic method format:
A:hasnextxxx () Determines whether it is a type of
B:nextxxx () returns an element of some type
(4) Two ways to master
a:public int Nextint ()
B:public String nextline ()
(5) Minor issues to be aware of
A: The same scanner object, get the value first, and then get the string there will be a small problem. The main reason: that is the problem of the newline symbol.
B: The solution:
A: Redefine a Scanner object, Scanner sc2 = new Scanner (;
B: Get all the data in a string and then convert it accordingly

C:sc.nextline ();//To clear the buffer of extra line breaks for emptying the line-break carriage return character
Examples, data types do not match:

Public class Scannerdemo {
Public static void Main (string[] args) {
//Create Objects
Scanner sc = new Scanner (;

//Get Data
if (Sc.hasnextint ()) {//enhanced robustness to prevent error: inputmismatchexception: The input does not match what you want
int x = Sc.nextint ();
System.out.println ("x:" + x);
} else {
System.out.println ("The data you entered is incorrect");

example, the integer gets completed, and then the string is empty, because the buffer also has an integer that captures the left line break

public class Scannerdemo {
     public static void Main (string[] args) {
         int b = Sc.nextint ();//Only the integer type is read, nothing else is intercepted, but the carriage return will leave a newline character in the buffer's
        sc.nextline ()//to clear newline carriage return character       
        string S1 = Sc.nextline ();
        system.out.println ("S1:" +s1+ ", B:" +b);
        system.out.println ("----------------");     

Overview and use of the 2:string Class (master)
(1) A string of data consisting of multiple characters.
In fact, it can be converted to a character array.
(2) Construction method:
A:public String ()
B:public String (byte[] bytes)
C:public String (byte[] bytes,int offset,int length)
D:public String (char[] value)
E:public String (char[] value,int offset,int count)
F:public string (string original)
The following is not a construction method, but the result is also a string object
g:string s = "Hello";
(3) Character of the string
A: Once the string is assigned, it cannot be changed.
Note: This refers to the contents of the string cannot be changed, not the reference cannot be changed.
B: Literals As String objects and different objects created by constructing methods
string s = new string ("Hello"); and string s = "Hello" the difference?
(4) The face question of the string (see program write result)
a:== and Equals ()
string S1 = new String ("Hello");
String s2 = new string ("Hello");
System.out.println (S1 = = s2);//False
System.out.println (s1.equals (S2));//True

String s3 = new String ("Hello");
String s4 = "Hello";
System.out.println (s3 = = S4);//False
System.out.println (S3.equals (S4));//True

String s5 = "Hello";
String s6 = "Hello";
System.out.println (S5 = = s6);//True
System.out.println (S5.equals (S6));//True
B: Concatenation of strings
String S1 = "Hello";
String s2 = "World";
String s3 = "HelloWorld";
System.out.println (S3 = = S1 + s2);//False
System.out.println (S3.equals ((S1 + s2));//True

System.out.println (s3 = = "Hello" + "world");//False This we are wrong, should be true
System.out.println (s3.equals ("Hello" + "world");//True
(5) function of string (self-completion method Chinese meaning)
A: Judging function
Boolean equals (Object obj)
Boolean equalsignorecase (String str)
Boolean contains (String str)
Boolean startsWith (String str)
Boolean endsWith (String str)
Boolean IsEmpty ()
B: Get Features
int Length ()
char charAt (int index)
int indexOf (int ch)
int indexOf (String str)
int indexOf (int ch,int fromIndex)
int indexOf (String str,int fromIndex)
String substring (int start)
String substring (int start,int end)
C: Conversion function
Byte[] GetBytes ()
Char[] ToCharArray ()
Static String valueOf (char[] CHS)
static String valueOf (int i)
String toLowerCase ()
String toUpperCase ()
String concat (String str)
D: Other functions
A: Replace function
String replace (char Old,char new)
String Replace (string old,string new)
B: Go to space function
String Trim ()
C: Compare Functions by Dictionary
int CompareTo (String str)
int comparetoignorecase (String str)
(6) Case of string
A: Impersonate A User Login
B: String traversal
C: Count the number of uppercase, lowercase, and numeric characters in a string
D: Turn the first letter of the string into uppercase, other lowercase
E: Stitching an int array into a string of the specified format
F: String inversion
G: Count the number of occurrences of a large string of small strings

Re-stepping on Java Road _day12 (scanner,string)

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: 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.