我不能在 String 参数上使用 toUpperCase() 方法,可能出了什么问题?
I can't use toUpperCase() method on a String parameter, what could be wrong?
我使用 Eclipse IDE 为 java 编程。
我写了一个 class 来显示一个名字是否在 CuncurrentHashMap 中,我的 IDE 没有显示任何错误,但是每当我 运行 我的程序时,我都没有得到我想要的输出。我想要的输出是将查询名称“Jerry”大写。我只是在学习 java 中的高级原理,我熟悉基本概念,但我愿意接受您对下面我的编码风格的任何纠正或批评。
package learnJavaPackages;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;
public class AddEmployee {
private String newEmployeeName;
private int empID=0;
ConcurrentHashMap<String, String> hashHandler = new ConcurrentHashMap<String, String>();
Scanner inputHere = new Scanner(System.in);
public void AddNewEmployee(){
System.out.print("Enter a new employee here: " );
newEmployeeName = inputHere.nextLine();
empID++;
String empIDstring = Integer.toString(empID);
newEmployeeName = newEmployeeName+empIDstring;
hashHandler.put(newEmployeeName, empIDstring);
}
public void showAddStatus(){
System.out.println(newEmployeeName +", has been added to the company");
}
public void showIsEmployeeIn(String isEmployee) {
isEmployee.toUpperCase();
if(hashHandler.containsKey(isEmployee)){
System.out.println(isEmployee +" is in the Company.");
}
else{
System.out.println(isEmployee +" is not in the company");
}
}
}
主要方法:
AddEmployee addEmpRef = new AddEmployee();
addEmpRef.AddNewEmployee();
addEmpRef.showAddStatus();
addEmpRef.showIsEmployeeIn("Jerry");
输出:
Enter a new employee here: Isaac
Isaac1, has been added to the company
Jerry is not in the company
.toUpperCase()
returns String
的全新实例,您不将其分配给任何变量。只是做:
isEmployee = isEmployee.toUpperCase();
字符串是不可变的。因此 sEmployee.toUpperCase()
不会更改 sEmployee
对象。相反,它 returns 一个新的 String
。使用
sEmployee = sEmployee.toUpperCase();
字符串是不可变的。结果,所有 "mutating" 方法都 return 更新后的字符串,您需要选择它。
isEmployee = isEmployee.toUpperCase();
我使用 Eclipse IDE 为 java 编程。 我写了一个 class 来显示一个名字是否在 CuncurrentHashMap 中,我的 IDE 没有显示任何错误,但是每当我 运行 我的程序时,我都没有得到我想要的输出。我想要的输出是将查询名称“Jerry”大写。我只是在学习 java 中的高级原理,我熟悉基本概念,但我愿意接受您对下面我的编码风格的任何纠正或批评。
package learnJavaPackages;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;
public class AddEmployee {
private String newEmployeeName;
private int empID=0;
ConcurrentHashMap<String, String> hashHandler = new ConcurrentHashMap<String, String>();
Scanner inputHere = new Scanner(System.in);
public void AddNewEmployee(){
System.out.print("Enter a new employee here: " );
newEmployeeName = inputHere.nextLine();
empID++;
String empIDstring = Integer.toString(empID);
newEmployeeName = newEmployeeName+empIDstring;
hashHandler.put(newEmployeeName, empIDstring);
}
public void showAddStatus(){
System.out.println(newEmployeeName +", has been added to the company");
}
public void showIsEmployeeIn(String isEmployee) {
isEmployee.toUpperCase();
if(hashHandler.containsKey(isEmployee)){
System.out.println(isEmployee +" is in the Company.");
}
else{
System.out.println(isEmployee +" is not in the company");
}
}
}
主要方法:
AddEmployee addEmpRef = new AddEmployee();
addEmpRef.AddNewEmployee();
addEmpRef.showAddStatus();
addEmpRef.showIsEmployeeIn("Jerry");
输出:
Enter a new employee here: Isaac
Isaac1, has been added to the company
Jerry is not in the company
.toUpperCase()
returns String
的全新实例,您不将其分配给任何变量。只是做:
isEmployee = isEmployee.toUpperCase();
字符串是不可变的。因此 sEmployee.toUpperCase()
不会更改 sEmployee
对象。相反,它 returns 一个新的 String
。使用
sEmployee = sEmployee.toUpperCase();
字符串是不可变的。结果,所有 "mutating" 方法都 return 更新后的字符串,您需要选择它。
isEmployee = isEmployee.toUpperCase();