Adan
Adan

Reputation: 473

Java: how to use variable in a method

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");
    }

Upvotes: 2

Views: 80

Answers (3)

Bruno Caceiro
Bruno Caceiro

Reputation: 7179

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;
        }


    }

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533442

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.

Upvotes: 0

colouredmirrorball
colouredmirrorball

Reputation: 331

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.

Upvotes: 4

Related Questions