Search code examples
javastringformattingconcurrenthashmap

I can't use toUpperCase() method on a String parameter, what could be wrong?


I use Eclipse IDE to program for java. I wrote a class to show whether a name is in a CuncurrentHashMap, my IDE does not show me any error, yet whenever I run my program, I do not get my desired output. My desired output is to have the queries name "Jerry" capitalised. I am only learning the advance principles in java, I am familiar with the basic concepts but I am open to any correction or criticism you have towards my coding style below.

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");
    }
}

}

main method:

AddEmployee addEmpRef = new AddEmployee();
    addEmpRef.AddNewEmployee();
    addEmpRef.showAddStatus();
    addEmpRef.showIsEmployeeIn("Jerry");

output:

Enter a new employee here: Isaac
Isaac1, has been added to the company
Jerry is not in the company

Solution

  • .toUpperCase() returns a brand new instance of String, which you don't assign to any variable. Just do:

    isEmployee = isEmployee.toUpperCase();