1. Console input (Scanner)
1.1 Scanner object
java.util.Scanner is a new feature of Java 5. We can get user input through scanner class.
The following is the basic syntax for creating Scanner objects:
Next, we demonstrate the simplest data input and obtain the input string through the next() and nextLine() methods of the Scanner class. Before reading, we generally need to use hasNext() and hasNextLine() to determine whether there is any input data. These two methods will first let the user input. If the data meets the requirements, it will return true and assign the user's input to the following nextxxx() function
public static void main(String[] args) { //Create a scanner object to receive keyboard data Scanner scanner = new Scanner(System.in); //Receive string in next mode (spaces are not allowed) System.out.println("Next Mode reception:"); //Determine whether the user has entered any characters if (scanner.hasNext()) { String str = scanner.next(); System.out.println("Input:" + str); } // All classes belonging to IO stream will always occupy resources if they are not closed Make it a good habit to turn it off when you run out // It's like you have to turn off the tap after you get the water Many download software or video software, if you don't turn it off completely, // You will upload and download by yourself, which takes up resources, and you will feel the card, which is a truth scanner.close(); }
Next, we use another method to receive data: nextLine()
public static void main(String[] args) { //Create a scanner object to receive keyboard data Scanner scanner = new Scanner(System.in); //Receive string in nextLine mode (can receive spaces) System.out.println("NextLine Mode reception:"); //Determine whether the user has entered any characters if (scanner.hasNextLine()) { String str = scanner.nextLine(); System.out.println("Input:" + str); } // All classes belonging to IO stream will always occupy resources if they are not closed Make it a good habit to turn it off when you run out // It's like you have to turn off the tap after you get the water Many download software or video software, if you don't turn it off completely, // You will upload and download by yourself, which takes up resources, and you will feel the card, which is a truth scanner.close(); }
1.2 difference between next and nextLine
next():
- Be sure to read valid characters before you can end the input.
- The next() method will automatically remove the whitespace encountered before entering valid characters.
- Only after entering a valid character will the blank space entered after it be used as a separator or terminator.
- next() cannot get a string with spaces.
nextLine(): - Take Enter as the ending character, that is, the nextLine() method returns all characters before entering carriage return.
- You can get blank.
1.3 other methods
If you want to input data of type int or float, it is also supported in the Scanner class, but you'd better use the hasNextXxx() method for verification before entering, and then use nextXxx() to read:
Example 01: determine the type of input data
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Receive data from keyboard int i = 0; float f = 0.0f; System.out.println("input data: "); if (scanner.hasNextInt()) { i = scanner.nextInt(); System.out.println("Integer type i = " + i); } else { System.out.println("The data entered is not an integer"); } if (scanner.hasNextFloat()) { f = scanner.nextFloat(); System.out.println("Floating Point Types i = " + f); } else { System.out.println("The data entered is not a floating point number"); } scanner.close(); }
Example 02: enter multiple numbers and sum and average them
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double sum = 0; //and int m = 0; //How many numbers have you entered //Judge whether there is any input through circulation, and sum and count each time in it while (scanner.hasNextDouble()) { double x = scanner.nextDouble(); m = m + 1; sum = sum + x; } System.out.println(m + "The sum of the numbers is" + sum); System.out.println(m + "The average number is" + (sum / m)); scanner.close(); }
2. Console output
2.1 standard output print and println
- print is normal standard output, but does not wrap.
- println is basically no different from print, but it will wrap in the end.
Example code:
public class Test { public static void main(String[] args) { System.out.print("Hello"); System.out.println(" World!"); System.out.print("------------"); } }
Operation results:
2.2 format output printf
You can use system out. The printf method displays formatted output on the console.
In many cases, you want to display values in some format. For example, the following code calculates interest for a given amount and annual interest rate.
double amount = 12618.98; double interestRate = 0.0013; double interest = amount * interestRate; System.out.println("Interest is S" + interest);
Operation results:
Because the interest amount is cash, you generally want to display only two digits after the decimal point of the floating-point value, so you can write the code as follows:
public static void main(String[] args) { double amount = 12618.98; double interestRate = 0.0013; double interest = amount * interestRate; System.out.println("Interest is S" + (int) (interest * 100) / 100.0); }
Operation results:
However, the format is still incorrect. Here should be two decimal places after the decimal point: 16.40, not 16.4. This problem can be corrected by using the Printf method, as follows:
double amount = 12618.98; double interestRate = 0.0013; double interest = amount * interestRate; System.out.printf("Interest is $%4.2f", interest);
explain:
The syntax for calling this method is:
System.out.printf(format, iteml, item2, ... ļ¼itemk)
The format here refers to a string composed of substring and format identifier.
The format identifier specifies how each entry should be displayed. The entries here can be numeric, character, Boolean, or string. A simple format identifier is a conversion code starting with a percent sign (%). Some common simple format identifiers and examples are listed below