为什么我不能在String参数上使用toUpperCase()方法?

时间:2016-06-29 07:52:35

标签: java string formatting concurrenthashmap

我使用Eclipse IDE为java编程。 我写了一个类来显示一个名字是否在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

3 个答案:

答案 0 :(得分:3)

.toUpperCase()会返回String的全新实例,您不会将其分配给任何变量。只是做:

isEmployee = isEmployee.toUpperCase();

答案 1 :(得分:3)

字符串是不可变的。因此sEmployee.toUpperCase()不会更改sEmployee对象。而是返回一个新的String。使用

sEmployee = sEmployee.toUpperCase();

答案 2 :(得分:0)

字符串是不可变的。因此,所有“变异”方法都会返回更新的String,您需要选择它。

isEmployee = isEmployee.toUpperCase();