0

I'm trying to make code that detects whether or not an email address is valid based on given conditions. My code seems to work for all the conditions except for when the email does not have an '@'. When it doesn't have an '@' I get a string out of bounds error. Any suggestions?

import java.util.Scanner;

public class email { public static void main (String[] args) {

//Create Scanner Object

Scanner stdin = new Scanner(System.in);

//prompt user for email address

System.out.print("Enter your email address: ");
String email = stdin.next();
email.length();

//shows location of @ and . characters

int atIndex = email.indexOf('@');
int finalperiodIndex = email.indexOf('.');
int periodIndex = email.indexOf('.');

//shows if email is valid or not

String invalid = "email is not valid";
String valid = "email is valid";
String location = email.substring(atIndex+1, email.length());
String userName = email.substring(0, atIndex);

//if the email does not have an '@' or '.' it is not valid

if((atIndex == -1) ||(periodIndex == -1)) {
    System.out.println(invalid);


//if the first character is a '@' it is not valid

}
else if (atIndex == 0) {
    System.out.println(invalid);

// if the last character is a '.' the email is invalid  

}
else if(periodIndex == email.length()-1) {
    System.out.println(invalid);
}
//if the final '.' is before the '@' the email is invalid

else if(finalperiodIndex < atIndex) {
    System.out.println(invalid);
}
//if there is a '.' before the '@' it's invalid

else if (email.indexOf('.') < email.indexOf('.')) {
    System.out.println(invalid);
}

//if there is no space between the '@' and '.' its invalid

else if (location.length()== -1) {
    System.out.println(invalid);
}
else {
    //end if 

    // print "valid" as well as domain and username

    System.out.println(valid);
    System.out.println("Username is: " + userName);
    System.out.println("Domain is: " + location);

1 Answers1

0

Move the if((atIndex == -1) ||(periodIndex == -1)) check higher up in your implementation, before your substring attempts. Also, make sure you stop the validation logic if this condition is true (not sure where this is implemented, but if it was a method then make sure to return false, or throw an exception, or whatever).

Daedalus
  • 1,667
  • 10
  • 12
  • Sorry this might be a dumb question but I am really new to java. How would I stop the validation logic if the condition is true? I also added the rest of the code if that helps. – Sam Schmitt Sep 26 '18 at 01:45
  • Since you are just putting this inside of a main method (ie: `public static void main(String [] args) { ... }`) then you can just use `return;` after printing out your `System.out.println(invalid);` statement. – Daedalus Sep 26 '18 at 01:48
  • I just tried adding the return; to it and I am still getting the same error. – Sam Schmitt Sep 26 '18 at 01:54
  • Might be easier to get "help me figure out Java"-style help like this on a different forum, like https://www.reddit.com/r/javahelp/ . – Daedalus Sep 26 '18 at 02:10