Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of the usage of Scanner in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is a summary of the usage of Scanner in Java. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Introduction to Scanner

Java 5 adds the java.util.Scanner class, a new utility for scanning input text.

It is some sort of combination between the previous StringTokenizer and Matcher classes. Since any data must be retrieved through capture groups of the same schema or by using an index to retrieve portions of text.

You can then combine regular expressions with methods for retrieving specific types of data items from the input stream. In this way, in addition to regular expressions, the Scanner class can arbitrarily parse data of strings and primitive types such as int and double.

Scanner allows you to write custom parsers for any text content you want to process.

Understanding nextInt(), next() and nextLine()

nextInt(): it only reads the int value, nextInt() places the cursor in the same line after reading the input. (nextInt() only reads the value, leaving "\n" unread, and puts the cursor in the row)

next(): read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input. (next() reads only the data preceding the space, and cursor points to this row)

The next() method starts scanning when it meets the first valid character (non-space, non-newline), and ends scanning when it meets the first delimiter or terminator (space or newline) to obtain the scanned content, that is, to obtain the first scanned single character string without spaces and newlines.

nextLine(): reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.

nextLine(), a line can be scanned and retrieved as a string.

public class NextTest{ public static void main(String[] args) { String s1,s2; Scanner sc=new Scanner(System.in); System.out.print("Please enter the first string: "); s1=sc.nextLine(); System.out.print("Please enter the second string: "); s2=sc.next(); System.out.println("The input string is: "+s1+" "+s2); } }

Results:

Please enter the first string: home

Please enter the second string: work

The input string is: home work

Modify the above procedure:

s1=sc.next(); s2=sc.nextLine();

Run Results:

Please enter the first string: home

Please enter the second string: Enter the string: home

As you can see, nextLine() automatically reads Enter removed by next() as its terminator, so there is no way to enter a value from the keyboard for s2.

After verification, I found that other next methods, such as double nextDouble(), float nextFloat(), int nextInt(), etc., have this problem when used with nextLine(). The solution is to add a nextLine() statement after each next(), nextDouble(), nextFloat(), nextInt(), etc. to filter out the Enter terminator removed by next().

public class NextTest{ public static void main(String[] args) { String s1,s2; Scanner sc=new Scanner(System.in); System.out.print("Please enter the first string: "); s1=sc.next(); sc.nextLine(); System.out.print("Please enter the second string: "); s2=sc.nextLine(); System.out.println("The input string is: "+s1+" "+s2); } }

Run Results:

Please enter the first string: home

Please enter the second string: work

The input string is: home work

Cycle input multiple sets of test cases

A while is a test case.

public static void main(String[] args){ Scanner in = new Scanner(System.in); //a while is a test case while(in.hasNext()){ int n = in.nextInt(); //Number of parameters received in the test case long[] array = new long[n]; String[] arrayStr = new String[n]; for(int i=0; i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report