Char cannot be dereferenced - JAVA


#1

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(System.in);
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);
else
  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.


#2

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


#3

So to fix this. Instead of

ssn.charAt(3).equals('-')

should it be

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


#4

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


#5

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