nextLine()函数获取用户输入的字符串?
The nextLine() function in Java is used to receive input from a user and retrieve a string from the keyboard. This function is commonly used when you want to prompt the user for some kind of input that could be multiple words or sentences. In this article, we will explore the details of the nextLine() function, including how it works and examples of how to use it.
How does the nextLine() function work?
The nextLine() function works by waiting for the user to input some data and press the Enter key. When the user presses the Enter key, the text is sent to the program, where the nextLine() function is waiting to capture it. The function then retrieves the entire line of text that was entered, including any spaces or special characters.
The nextLine() function is part of the Scanner class in Java. The Scanner class provides many functions for reading and parsing data from the keyboard or other input sources. The nextLine() function is specifically tailored to retrieve a line of text from the keyboard.
Syntax of nextLine() function
The syntax of the nextLine() function is as follows:
scanner.nextLine();
The nextLine() function is called using the Scanner object that you created earlier in your program. It does not take any arguments.
Examples of using nextLine() function
Example 1: Getting a name from the user
In this example, we will use the nextLine() function to retrieve a user's name from the keyboard. The program will prompt the user for their name, and then display a greeting that includes their name.
import java.util.Scanner;
public class GetName {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("What is your name? ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
}
}
Output:
What is your name? John
Hello, John!
Explanation:
In this program, we first create a new Scanner object called scanner. We then use the System.out.print() function to display a prompt asking the user for their name. Once the user has entered their name and hit Enter, the nextLine() function is called to retrieve the input and store it in the name variable.
The program then outputs a greeting that includes the user's name using System.out.println().
Example 2: Capitalizing the first letter of a sentence
In this example, we will use the nextLine() function to retrieve a sentence from the keyboard and capitalize the first letter of the sentence.
import java.util.Scanner;
public class CapitalizeFirstLetter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = scanner.nextLine();
String firstLetter = sentence.substring(0, 1);
String restOfSentence = sentence.substring(1);
String capitalizedSentence = firstLetter.toUpperCase() + restOfSentence.toLowerCase();
System.out.println("Capitalized sentence: " + capitalizedSentence);
}
}
Output:
Enter a sentence: the quick brown fox jumps over the lazy dog.
Capitalized sentence: The quick brown fox jumps over the lazy dog.
Explanation:
In this program, we first create a new Scanner object called scanner. We then prompt the user to enter a sentence and use the nextLine() function to retrieve the input.
We then use the String.substring() function to extract the first letter and the rest of the sentence. We capitalize the first letter using the String.toUpperCase() function and convert the rest of the sentence to lowercase using the String.toLowerCase() function.
Finally, we output the capitalized sentence using System.out.println().
Conclusion
The nextLine() function in Java is a simple but powerful way to retrieve input from a user. It allows you to retrieve entire lines of text, including spaces and special characters. It is commonly used to prompt the user for input, validate user input, and manipulate strings. With the examples provided in this article, you should be able to start using nextLine() in your own Java programs.
