Char cannot be dereferenced
Hi everyone. I'm a beginner at Java and I have some code that keeps giving me an error.
Code :
import java.util.Scanner;
public class Assignment3
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
String firstName, middleName, lastName;
char firstInitial, middleInitial, lastInitial;
System.out.println("What is your first name? ");
firstName = scan.nextLine();
System.out.println("What is your middle name? ");
middleName = scan.nextLine();
System.out.println("What is your last name? ");
lastName = scan.nextLine();
firstInitial = firstName.charAt(0);
middleInitial = middleName.charAt(0);
lastInitial = lastName.charAt(0);
System.out.println("Your initials are " + firstInitial.toUpperCase() + middleInitial.toUpperCase() + lastInitial.toUpperCase());
System.out.println("Your name is " + lastName.toUpperCase() + ", "+firstInitial + "." + middleInitial + ".");
}
}
I get the error java:32: char cannot be dereferenced at the periods before toUpperCase in the line about initials, but not in the one about the last name, if that makes sense. I've been staring at it for like half an hour and I just can't figure it out. Can someone help me out? Thanks!
Re: Char cannot be dereferenced
char is a primitive and String is an object, so you are trying to call a method on a primitive that does not exist. Solution would be to use the Character.toUpperCase method or Character.toString() method then call toUpperCase
Re: Char cannot be dereferenced