我是java的初学者,我正在网上做练习题。我尝试过这个问题,但我不明白这个错误。
编写一个名为 processName 的方法,该方法接受控制台的 Scanner 作为参数并提示用户输入他或她的全名,然后以相反的顺序打印名称(即姓氏、名字)。您可以假设只会给出名字和姓氏。您应该使用扫描仪一次读取整行输入,然后根据需要将其拆分。这是与用户的示例对话:
请输入您的全名:Sammy Jankis 您的名字倒序是 Jankis,Sammy
public static void processName(Scanner console) {
System.out.print("Please enter your full name: ");
String full=console.nextLine();
String first=full.substring(0," ");
String second=full.substring(" ");
System.out.print("Your name in reverse order is: "+ second + "," + first);
}
也许我会解释我的代码。所以我尝试将这两个词分开。所以我使用子字符串来找到这两个词,然后我硬编码来反转它们。我认为逻辑是正确的,但我仍然得到这些错误。
Line 6
You are referring to an identifer (a name of a variable, class, method, etc.) that is not recognized. Perhaps you misspelled it, mis-capitalized it, or forgot to declare it?
cannot find symbol
symbol : method substring(int,java.lang.String)
location: class java.lang.String
String first=full.substring(0," ");
^
Line 7
You are referring to an identifer (a name of a variable, class, method, etc.) that is not recognized. Perhaps you misspelled it, mis-capitalized it, or forgot to declare it?
cannot find symbol
symbol : method substring(java.lang.String)
location: class java.lang.String
String second=full.substring(" ");
^
2 errors
33 warnings