In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use Scanner and hasNextXXX () in Java, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.
Input / output
Basic grammar
System.out.println (msg); / / output a string with newline System.out.print (msg); / / output a string without newline System.out.printf (msg); / / format the output, which is the same as C language
For example:
Public class SannerDemo {public static void main (String [] args) {System.out.println ("hello world!"); System.out.print ("hello world!"); String str = "hello world"; System.out.printf ("% s\ n", str);}}
Shortcut key recommendation: here, if you are using IDEA, you can enter sout and enter, which will automatically output System.out.println ()
Input read using Scanner
First you need to import the package of = = import java.util.Scanner;==, and then Scanner sc = new Scanner (System.in);, the main purpose of this code is to read data from keyboard input.
Then read the data:
The difference between next (), nextInt () and nextLIne ()
Import java.util.Scanner;public class SannerDemo {public static void main (String [] args) {Scanner sc = new Scanner (System.in); int I = sc.nextInt (); System.out.println (I); / read int data / / read a row of data String S1 = sc.nextLine (); System.out.println (S1) / / read the string String S2 = sc.next (); System.out.println (S2);}
NextInt ():
Int I = sc.nextInt (); System.out.println (I); / / read int data
You can read numbers, but when you encounter spaces, you can only read numbers before spaces.
Next ():
/ read the string String S2 = sc.next (); System.out.println (S2)
You can read strings, but when you encounter spaces, you can only read numbers before spaces.
NextLine ():
/ / read a row of data String S1 = sc.nextLine (); System.out.println (S1)
You can read the string and read this line, but the carriage return ends.
Note:
Next () and nextLine () cannot be used at the same time:
For example:
/ / read the string String S2 = sc.next (); System.out.println (S2); / / read a line of data String S1 = sc.nextLine (); System.out.println (S1)
This only outputs one line, because nextLine () reads the carriage return and ends it.
Next () will end when it encounters an empty passenger.
Use Scanner Loop to read N digits / strings
Use of hasNextInt ()
Import java.util.Scanner;public class SannerDemo {public static void main (String [] args) {Scanner sc = new Scanner (System.in); while (sc.hasNextInt ()) {int I = sc.nextInt (); / / enter the number I System.out.println (I); / / print the number I}}
When the program starts, it will cycle through and print a number until the Ctrl+d ends the program.
Here the result of sc.hasNextInt () is a type of boolean, when the result is false.
Note:
Ctrl+d is used to end the loop and enter multiple data.
By the same token:
All of these methods can be used for circular data input.
One point to note about nextxxx () in Scanner public static void main (String [] args) {/ / TODO code application logic here Scanner s = new Scanner (System.in) / / it should be noted that if the integer is read through nextInt (), and then the string is read, it reads the carriage return newline: "\ r\ n", because nextInt only reads the numeric information and does not read the carriage return newline "\ r\ n". / / so, if you need to read integers and then read strings in business, nextLine () should be executed twice in a row, the first time to take the integer, and the second time to read the real string int I = s.nextInt (); System.out.println ("read integer is" + I "); String rn = s.nextLine () / / read the space String a = s.nextLine (); / / read the string System.out.println ("read the string is:" + a);} above is all the content of the article "how to use Scanner and hasNextXXX () in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.