0

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
mabwehz
  • 116
  • 1
  • 8
  • Hi @Thilo. Thanks for reviewing this question. With due sincerity I could not find a question similar(duplicate-wise) to mine. Maybe I'm still new to the way things work here, pardon me. – mabwehz Jun 30 '16 at 01:49
  • The link to the duplicate is included in the yellow box right on top of this page, as well as in the "Linked" section on the right. – Thilo Jun 30 '16 at 02:31

3 Answers3

3

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

isEmployee = isEmployee.toUpperCase();
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
3

String is immutable. Thus sEmployee.toUpperCase() does not change the sEmployee object. Instead it returns a new String. Use

sEmployee = sEmployee.toUpperCase();
René Link
  • 48,224
  • 13
  • 108
  • 140
1

Strings are immutable. As a result, all "mutating" methods return the updated String instead, and you need to pick that up.

isEmployee = isEmployee.toUpperCase();
Thilo
  • 257,207
  • 101
  • 511
  • 656