Char cannot be dereferenced - JAVA

import java.util.Scanner;

public class SSN{
public static void main(String args){

System.out.print("Enter a social security number: ");
Scanner input = new Scanner(;
String ssn = input.nextLine();
boolean B1 = Character.isDigit(ssn.charAt(0)) && Character.isDigit(ssn.charAt(1));
boolean B2 = Character.isDigit(ssn.charAt(2)) && ssn.charAt(3).equals('-');
boolean B3 = Character.isDigit(ssn.charAt(4)) && Character.isDigit(ssn.charAt(5));
boolean B4 = ssn.charAt(6).equals('-') && Character.isDigit(ssn.charAt(7));
boolean B5 = Character.isDigit(ssn.charAt(8)) && Character.isDigit(ssn.charAt(9));
boolean B6 = Character.isDigit(ssn.charAt(10));                                

if ((B1 && B2) && (B3 && B4) && (B5 && B6))
  System.out.printf("%s is a valid social security number.", ssn);
  System.out.printf("%s is a invalid social security number.", ssn);

Please help. I have no idea what is wrong. Thanks. The problem is with the line declaring B2 and B4.

char is a primitive, it has no equals method

Also the parentheses in your if-statement are very odd, they do not significantly change the order that the expression is evaluated in, doesn’t clarify it either. Those parentheses make it look like perhaps you meant something else there

So to fix this. Instead of


should it be

(int) ssn.charAt(3) == \u002D

What’s the motivation for each of those changes? Seems like in order to water the flowers you also plowed the field and repainted the house

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.