我在以下方面需要帮助:
- 使用 Scanner 类接收输入(我明白了)
- 从扫描仪获取输入并将其变成字符串
- 使用
replaceAll
从用户输入中删除数字 0-9。
下面的代码是我到目前为止的代码,但它只返回用户输入而不删除数字:
public static void main(String[] args) {
Scanner firstname = new Scanner(System.in);
System.out.println("Please enter your first name:");
String firstname1 = firstname.next();
firstname1.replaceAll("[^0-9]","");
System.out.println(firstname1);
更新代码。谢谢气垫船。我现在正在研究如何使用下面的代码检索所有字母字符,我只取回用户输入的数值之前的字母:
import java.util.Scanner;
public class Assignment2_A {
public static void main(String[] args) {
Scanner firstname = new Scanner(System.in);
System.out.println("Please enter your first name:");
String firstname1 = firstname.next();
firstname1 = firstname1.replaceAll("[^A-Z]","");
System.out.println(firstname1);
最佳答案
String input = yourScannerObject.nextLine ();
其中“yourScannerObject”是您为扫描仪指定的名称。
关于java - 如何获取扫描仪对象并使其成为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17514930/