im new to java and am writing a program that will let you input your first and last name and will give you your initials, but i want the intitials to always be in upper case.
I get a "char cannot be dereferenced" error whenever i run the code.
import java.util.*;
public class InitialHere
{
public static void main (String[] args)
{
Scanner getInput = new Scanner (System.in);
String firstName;
String lastName;
char firstInitial;
char lastInitial;
System.out.println("What is your first name?");
System.out.println();
firstName = getInput.nextLine();
System.out.println();
System.out.println("Thankyou, what is your last name?");
System.out.println();
lastName = getInput.nextLine();
firstInitial = firstName.charAt(0);
lastInitial = lastName.charAt(0);
firstInitial = firstInitial.toUpperCase();
lastInitial = lastInitial.toUpperCase();
System.out.println();
System.out.println("Your initials are " + firstInitial + "" + lastInitial + ".");
}
}
In Java, primitives have no methods, only their boxed types do. If you want to get the uppercase version of a char
, the Character
class has a method just for that: Character.toUpperCase
:
firstInitial = Character.toUpperCase(firstInitial);
(Do the same for lastInitial
)