hi, I'm a first-level java learner, so my questions might seem really basic. but I don't really have a clue as to what I'm doing wrong. Can you guys please help out?

I'm supposed to write a program that ask the user to input their email address. and The first letter and the last letter cannot be '@'. Moreover, '@' is supposed to be included somewhere in the middle of the input.
I'm asking the user to input the data in PersonDB class. and I'm trying to write the validator for the email in another class called the Validator.

so in the PersonDB class, I have:

System.out.println("Enter Email Address: ");
String email = sc.next();
int strLength = email.length();
System.out.println("" + strLength);
c.setEmail(email);

and in the Validator class, I have:

boolean validateEmail(String emailValid)
	{
		isvalid = false;
		while (emailValid[0] =='@' || emailValid[emailValid - 1] == '@')
		{
			System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address.\n");
		}
		
		if (emailValid[0] != '@' && emailValid[strLength - 1] != '@')
		{
			if (int index = 0, index < strLength, index++)
			{
				//don't know what to do here
			}
		}
		
		return isvalid;
	}

I know this is not right. But I don't have a clue as to what I should do.

Ok, I think I see what you're having trouble with....You don't know what to do when it pasts the first test(first and last character are not @), but you're not sure how to check if it's somewhere in the middle....As soon as you find one you could return true:

if (int index = 1, index < strLength-1, index++)
	{
                if (emailValid[index].equals("@"))
                {
                        return true;
                }
                 else
                 {
                          if (index == strLength-2)
                         {
                               return false;
                         }
                 }
	}

The second problem you have is using ==. This might work, but I'm not sure that it will. Since you are comparing string values(not string objects) you need to use the .equals() method.

Hi,
Following your advices, I've made the changes to my program accordingly. But I still have errors that I don't know how to fix.

PersondDB code:

//String email_String = Validator.getString(sc, "Enter Email Address: ");
System.out.println("Enter Email Address: ");
String email = sc.next();
int strLength = email.length();
System.out.println("" + strLength);
Validator.validateEmail(email, strLength);
c.setEmail(email);

Validator code:

public boolean validateEmail(String emailValid, int strLength)
	{
		isvalid = false;

		while ((emailValid[0].equals('@'))
				|| (emailValid[emailValid - 1].equals('@')))
		{
			System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address.\n");
		}

			if (int index = 1, index < strLength-1, index++)
			{
		                if (emailValid[index].equals("@"))
		                {
		                        return true;
		                }
		                 else
		                 {
		                          if (index == strLength-2)
		                         {
		                               return false;
		                         }
		                 }
	}

		return isvalid;
	}

And this is the list of errors I have:

.\Validator.java:23: '.class' expected
if (int index = 1, index < strLength-1, index++)
^
.\Validator.java:38: ')' expected
return isvalid;
^
X:\java CS170\Lab 2b\PersonDB.java:33: non-static method validateEmail(java.lang.String,int) cannot be referenced from a static context
Validator.validateEmail(email, strLength);
^
.\Validator.java:15: cannot find symbol
symbol : variable isvalid
location: class Validator
isvalid = false;
^
.\Validator.java:17: array required, but java.lang.String found
while ((emailValid[0].equals('@'))
^
.\Validator.java:18: operator - cannot be applied to java.lang.String,int
|| (emailValid[emailValid - 1].equals('@')))
^
.\Validator.java:18: array required, but java.lang.String found
|| (emailValid[emailValid - 1].equals('@')))
^
.\Validator.java:23: unexpected type
required: value
found : class
if (int index = 1, index < strLength-1, index++)
^
8 errors

Hi,

There are still several problems in the code, please see comments below:

// You do not need to pass in the length, a String object already knows its own length
// Also you appear to be 'stuck' in a static context (in 'main' I expect), you will need to make this method static or create an Object instance in 'main'
public boolean validateEmail(String emailValid, int strLength)
{
   // This is not declared and also never altered, why not just 'return false' at the end
   isvalid = false;

   // This should not be a 'while' loop as it is a one off check, use an 'if' instead
   // emailValid - 1 should be emailValid.length() - 1
   while ((emailValid[0].equals('@')) || (emailValid[emailValid - 1].equals('@'))) {
	  System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address.\n");
   }

   // This should be a 'for' loop not an 'if' statement
   // You should really brush up on your control statements. I.E. if, while, for etc ...
   if (int index = 1, index < strLength-1, index++) {
	  if (emailValid[index].equals("@")) {
		 return true;
	  }
	  // Why not just loop until strLength-2
	  else {
		 if (index == strLength-2) {
			return false;
		 }
	  }
   }

   // Just return false instead
   return isvalid;
}

A modified version is below:

public boolean validateEmail (String asAddress)
{
   if (asAddress == null) { return false; }

   if ((asAddress[0].equals('@')) || (asAddress[(asAddress.length()-1)].equals('@'))) {
	  System.out.println("ERROR! you cannot enter @ at the beginning or the end of your email address.");
   }

   for (int index = 1, index < (asAddress.length()-2), index++) {
	  if (asAddress[index].equals("@")) {
		 return true;
	  }
   }

   return false;
}

Or better yet would be to use the String Object's own methods.

public boolean validateEmail (String asAddress)
{
   if (asAddress == null) {
	  System.out.println("Error: Email Address: Is null");
	  return false;
   }

   if (asAddress.startsWith('@')) {
	  System.out.println("Error: Email Address: Starts with '@'");
	  return false;
   }

   if (asAddress.endsWith('@')) {
	  System.out.println("Error: Email Address: Ends with '@'");
	  return false;
   }

   if (asAddress.indexOf('@') == -1) {
	  System.out.println("Error: Email Address: Must contain '@'");
	  return false;
   }

   return true;
}

I am not usually in the habit of giving complete answers as code, and will likly get flamed for doing so (I was bored). So please research the methods used above from the URL below or you will learn nothing!!, which would be partly my fault.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

Also as I mentioned in the code comments, you really need to get to grips with ... if, for, while etc ...

Kate

hi hooknc,
Thanks for suggestion. I didn't know about the endsWith and the startsWith methods. Please don't feel guilty. I'm a visual learner. I learn by example. That's why I sometimes doubt programming is the right thing for me. your code helps me a lot. I read the articles...don't really understand everything, but I know that all those methods exist now.

oh I also want to point out that:
if (asAddress.startsWith('@')).....

should use the double quote around @, like this if (asAddress.startsWith("@"));

else the compiler throws errors.

I'm sure you know that already. I'm pointing it out for anyone else who might be having the same problem and reviewing this thread.

I don't understanding one thing though:

in the following code

if (asAddress.indexOf("@") == -1) {
		  System.out.println("Error: Email Address: Must contain '@'");
		  return false;
	   }

why is it -1?

Thanks again,
Karen

The -1 means that the string that you're searching for isn't found inside the string being searched.

Regards,

Nate

This article has been dead for over six months. Start a new discussion instead.