2

The code inspects the chars in string invemail ("sammy99gmail.com"). notice how the email is missing the '@' sign, this program should tell if it's there or not, if so, print invemail "is a valid email" else invemail "is not valid".

The problem is that i get an error highlighted at 'emailfound' on the second 'if' statement. what am i doing wrong?

code:

String invemail = "sammy99gmail.com";


    for (int i = 0; i < invemail.length(); i++)
    {
        char emailfind = invemail.charAt(i);
        if (emailfind == '@')
        {
            boolean emailfound = true;
        }

        else
        {
            boolean enotfound = false;
        }


    }

    if (emailfound = true)
    {
        System.out.println(invemail + "is a valid email");
    }
    else
    {
        System.out.println(invemail + "is invalid");
    }
2
  • 1
    You are setting variables which have no scope and you are not checking whether emailfound == true or just if (emailfound) but instead you are assigning emailfound = true and then checking is true is still true. Commented Aug 30, 2015 at 15:15
  • ah good spotting, changed it to (emailfound) Commented Aug 30, 2015 at 15:21

3 Answers 3

4

If it can't find an @ sign, the boolean emailfound would not get declared at all! So the compiler gives an error saying it's possible emailfound does not exist at that part of the code. To solve this simply declare the boolean outside the first if statement so that it still exists when the second if statement is reached:

boolean emailfound = false;
for(int i = 0; i < invemail.length(); i++)
//etc

By setting it to false by default, it means you don't need to set it to false when it can't find the @ symbol, so you wouldn't need the else here. Be careful that when you found an @, you should assign true to emailfound without declaring a new one: emailfound = true; instead of doing boolean emailfound = true; as that would result in an error about duplicate local variable.

Another thing I spotted is in the second if statement: if (emailfound = true). Careful here! You used = instead of == which means it will actually assign the value true to emailfound instead of evaluating if it's equal to true! In fact you don't need to compare it at all, the if statement expects a boolean and emailfound is a boolean. It's always better to do if(emailfound) instead of if(emailfound == true) precisely to avoid this mistake.

Sign up to request clarification or add additional context in comments.

Comments

0

You can write this with a boolean, using a label.

String invemail = "sammy99gmail.com";

found: {
    for (int i = 0; i < invemail.length(); i++) {
        char emailfind = invemail.charAt(i);
        if (emailfind == '@') {
            System.out.println(invemail + "is a valid email");
            break found;
         }
    }
    System.out.println(invemail + "is invalid");
}

You can avoid using a label by defining a method to perform a check.

1 Comment

It'd be better practice to put the code in a method rather than using the obscure break statement-block style.
0

You are declaring emailfoundinside an if statement. By doing this, the variable is one visible inside that statement. Since you are accessing emailfoundin other section of the code, the error you get is because the variable is not globaly declared.

Change to:

String invemail = "sammy99gmail.com";
   boolean emailfound = false;
   boolean enotfound = false;

    for (int i = 0; i < invemail.length(); i++)
    {
        char emailfind = invemail.charAt(i);
        if (emailfind == '@')
        {
            emailfound = true;
        }

        else
        {
            enotfound = false;
        }


    }

4 Comments

It seems like this will generate a variable emailfound may not have been initialized error from the compiler.
You need to initialize the variable too. Otherwise the read access in the condition of the if after the loop will cause a compile time error.
boolean emailfound = false;
Thanks, I missed the variable initialization. Fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.