How to input numbers and strings in Scanner in java
This article is about how Scanner inputs numbers and strings in java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Introduction to Scanner Class
Java 5 adds the java.util.Scanner class, a new utility for scanning input text. It is some kind of combination between the previous StringTokenizer and Matcher classes. Because any data must be retrieved through a capture group of the same pattern or by using an index to retrieve parts of the text. You can then combine regular expressions with methods to retrieve specific types of data items from the input stream. In this way, in addition to using regular expressions, the Scanner class can analyze data of string and basic types such as int and double at will. With Scanner, you can write a custom parser for any text content you want to work with.
Scanner input numbers, strings
The functions of package java05;import java.util.Scanner;//1, guide package / * Scanner class, which can be used to input data by keyboard and refer to the type in the program.
1. Guide package
2. Create
3. Use to get an int number entered by the keyboard, int num = sc.nextInt () to get a str string entered by the keyboard, String str = sc.next () * * / public class DemoScanner {public static void main (String [] args) {/ / create / / Note: System.in represents input from the keyboard Scanner sc = new Scanner (System.in); / / 3, get the int number entered by the keyboard int num = sc.nextInt () System.out.println ("the input number is:" + num); / / gets the keyboard input string String str = sc.next (); System.out.println ("the input string is:" + str);}}
Thank you for reading! This is the end of the article on "how to input numbers and strings in Scanner in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!