jsmarsh
jsmarsh

Reputation: 21

int cannot be converted to charsequence

I am trying to loop through my arraylist and print email addresses that are invalid. To be invalid, the address does not contain an @ sign or a period.

I am getting an error, int cannot be converted to charsequence for my method.

public static void print_invalid_emails(){
    for (int i = 0; i < studentList.size(); i++) {
        if (studentList.get(i).getEmail().contains('@' + '.')){
            System.out.println("Scanning Roster");
        }
        else {
            System.out.println("Invalid email address, " + studentList.get(i).getEmail());

        }
    }
}

Upvotes: 2

Views: 5288

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

The problem is that Java treats '@' + '.' as an addition of two characters, not as a concatenation of a string. The result of this addition is UNICODE code point of @ plus unicode code point of ., expressed as an int.

If you want to check that the item is missing a . or a @, you should perform these two checks in two separate calls of contains(...)

Upvotes: 2

The bug is here:

if (studentList.get(i).getEmail().contains('@' + '.')){

It should be:

if (studentList.get(i).getEmail().contains("@") || studentList.get(i).getEmail().contains(".")){

Upvotes: 1

Related Questions