Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Username/Password Different Classes Java program help

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Username/Password Different Classes Java program help

    I have to create two different classes one for the username and one for the password. Im going to focus on the username first. So the username will be a valid email address meaning the username should have the '@' symbol embedded within the submitted string somewhere. It should also have the domain referencer (the '.' character) and the only acceptable domains should be .com, .edu, .org, .mil, and .gov for now.So my program should check to see if these symbols exist and the '.' is indeed the fourth charcacter from the end of the string. Please I need help getting started. I am new to java and please anything would help. I tried looking on how to do it but no luck so if anyone would help. Explain how and why so i can understand it. Thanks in advance.


  2. #2
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Username/Password Different Classes Java program help

    Hi,

    You can validate email address using regular expression. Google it and you will be able to find it.

  3. #3
    Junior Member didingnuriska's Avatar
    Join Date
    Apr 2013
    Posts
    21
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Username/Password Different Classes Java program help

    I think you just need one class, ie: User
    make 2 variable username and password as String
    for username (email) validation,
    I think you can go this way,
    you can extract the username variable to validate
    1 @
    2 .com, .org, etc

    you can user chartAt(int index) to get the chart in username.
    simply use looping operation to get all char or string u need.
    ie: for (int i=0;i<username.length():i++){
    // extract the char/string that you need by using charAt
    }

    if the extraction process is done..
    then you can validate for case 1 and case 2.

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Email validation Java program help

    Okay so far I have this but i am not getting the right results as wanted. Please look the code and see what im doing wrong.
     
    import java.util.Scanner; // Imports the Scanner class to get Keyboard Inputs
     
    		class ValidateUser{
     
    		public static void main (String [] args) {
     
    		String inputUserName; // Creates the userName variable
     
    		Scanner input = new Scanner (System.in); // Creates a new Scanner
     
    		System.out.print("Username: "); // Prints the word "Username" to the screen
    		inputUserName = input.next(); // Gets the user input for the username
     
    		System.out.println(UserCheck(inputUserName)); // Calls the UserCheck Method
    		System.out.println("");
     
    		main(args); // re-runs the program (Allows for multiple tests)
    		}
     
    		public static String UserCheck (String Username) {
     
    		String result = "Valid Username"; // Sets the initial result as valid
    		int atLoc = 0; // Sets initial (@) location to be 0
    		int perLoc = 0; // Sets initial period location to be 0
    		int atCount = 0;// Keeps track of the number of (@) symbols
    		int perCount = 0; // Keeps track of the number of periods
     
    		for (int x =0; x < Username.length(); x++) {
    		if ((Username.charAt(x) >= 48 && Username.charAt(x) <= 57) || (Username.charAt(x) >= 65 && Username.charAt(x) <= 90) 
    				|| (Username.charAt(x) >= 97 && Username.charAt(x) <= 122) || (Username.charAt(x) == 64) ||
    				(Username.charAt(x) == 46) || (Username.charAt(x) == 95)) { //Keep the Username } else { result = "Username Contains Invalid Character!"; //Checks that password contains only letters and numbers } if (Username.charAt(x) == 64) { //Finds the location of the (@) symbol atLoc = x; //and counts the number of (@) symbols in username atCount++;
    		} if (Username.charAt(x) == 46) { 
    			result = "valid";//Finds the location of the period perLoc = x; //and counts the number of periods in username perCount++;
    		}
    		} // Ends the for loop
     
    		if ((atLoc < 1) || (perLoc < 1) || (perLoc == Username.length()-1)) { //Checks that email contains a period and an (@) symbol and
    		result = "Not a valid email address"; //that they are not in the first or last position in the Username.
    		}
     
    		if (perLoc < atLoc) {
    		result = "Not a valid email address"; //Checks that the (@) symbol follows the period in the username
    		}
     
    		if ((atCount > 1) || (perCount > 1)) { //Checks that there is only one (@) symbol and one period in a username
    		result = "Not a vaild email address";
    		}
     
    		if ((perLoc - atLoc) <= 1) {
    		result = "Not a vaild email address";
     
    		}
    		return (result);// Returns the value of "result"
     
    		} // Ends the UserCheck method
    		}//Ends the ValidateUser Class


    OUTPUT:
    Username: tom@gmail.com
    Not a vaild email address
     
    Username: Ij@@.com
    Not a vaild email address
     
    Username: Jess@hotmail.com
    Not a vaild email address
     
    Username:


    I keep getting "Not a valid email adress" I am not sure why. PLease help

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Username/Password Different Classes Java program help

    There are multiple places that error message is used? Which one is setting that error message?
    Either change all the error messages so they are unique so you will know where it came from,
    or add a println() statement just before each setting of the message that prints out the reason that the error message is being set.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Username/Password Different Classes Java program help

    Okay I did that and found out the last one that says Not a valid email address is the one that always pops up in the output.

    import java.util.Scanner; // Imports the Scanner class to get Keyboard Inputs
     
    		class ValidateUser{
     
    		public static void main (String [] args) {
     
    		String inputUserName; // Creates the userName variable
     
    		Scanner input = new Scanner (System.in); // Creates a new Scanner
     
    		System.out.print("Username: "); // Prints the word "Username" to the screen
    		inputUserName = input.next(); // Gets the user input for the username
     
    		System.out.println(UserCheck(inputUserName)); // Calls the UserCheck Method
    		System.out.println("");
     
    		main(args); // re-runs the program (Allows for multiple tests)
    		}
     
    		public static String UserCheck (String Username) {
     
    		String result = "Valid Username"; // Sets the initial result as valid
    		int atLoc = 0; // Sets initial (@) location to be 0
    		int perLoc = 0; // Sets initial period location to be 0
    		int atCount = 0;// Keeps track of the number of (@) symbols
    		int perCount = 0; // Keeps track of the number of periods
     
    		for (int x =0; x < Username.length(); x++) {
    		if ((Username.charAt(x) >= 48 && Username.charAt(x) <= 57) || (Username.charAt(x) >= 65 && Username.charAt(x) <= 90) 
    				|| (Username.charAt(x) >= 97 && Username.charAt(x) <= 122) || (Username.charAt(x) == 64) ||
    				(Username.charAt(x) == 46) || (Username.charAt(x) == 95)) { //Keep the Username } else { result = "Username Contains Invalid Character!"; //Checks that password contains only letters and numbers } if (Username.charAt(x) == 64) { //Finds the location of the (@) symbol atLoc = x; //and counts the number of (@) symbols in username atCount++;
    		} if (Username.charAt(x) == 46) { 
    			result = "valid";//Finds the location of the period perLoc = x; //and counts the number of periods in username perCount++;
    		}
    		} // Ends the for loop
     
    		if ((atLoc < 1) || (perLoc < 1) || (perLoc == Username.length()-1)) { //Checks that email contains a period and an (@) symbol and
    		result = "Not a valid email address"; //that they are not in the first or last position in the Username.
    		}
     
    		if (perLoc < atLoc) {
    		result = "Not a valid email address"; //Checks that the (@) symbol follows the period in the username
    		}
     
    		if ((atCount > 1) || (perCount > 1)) { //Checks that there is only one (@) symbol and one period in a username
    		result = "Not a vaild email address";
    		}
     
    		if ((perLoc - atLoc) <= 1) {
    		result = "Not a vaild email address";
     
    		}
    		return (result);// Returns the value of "result"
     
    		} // Ends the UserCheck method
    		}//Ends the ValidateUser Class

    Still not sure what to do.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Username/Password Different Classes Java program help

    last one that says Not a valid
    Why does the last one say there is an error? What tests are made? What are the values of the variables used in the last one?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member didingnuriska's Avatar
    Join Date
    Apr 2013
    Posts
    21
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Username/Password Different Classes Java program help

    Sorry for incomplete explanation..
    I think you almost got it..
    but I think you can do this in simple way..
    1 @ case
    ==
    you need to make a boolean function that return true if the username(String) contain '@'
    private boolean checkforcase1(){
    for(int i=0;i<username.length;i++){
    //check the '@' character
    //if it's there simply return true
    }
    return false;
    }

    if true, then you need to check
    2. .com, .org etc case
    ===
    first you have to find index where '.' is located, make a int function to get the location of '.'
    if you got the location
    now you need to get the string('.com','.org' or etc) from username, you can use one of String method, substring(int begin, int end)
    assume begin is the '.' location and end is the length of username.
    if the string is got, now you can compare with your predefine string array['.com','.org', etc] that you accepted..
    if there is you can say that's a valid else invalid
    the main flow diagram is like this
    1. if case 1 is true than execute case 2 else return false and enter the invalid email address
    2. if case 2 is return true you can print valid else invalid

    There is more you can do with String (method), I suggested you to "try and error" which one you like...

Similar Threads

  1. Can Someone please help me with my password program?
    By mkrage in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2012, 11:16 AM
  2. Check Password Program
    By m2msucks in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 6th, 2011, 01:39 AM
  3. Database settings (username and password)
    By palooza in forum JDBC & Databases
    Replies: 3
    Last Post: February 24th, 2011, 01:53 AM
  4. Password Program
    By computercoder in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 20th, 2010, 07:03 AM
  5. Connecting to a HTTPS URL using username and password
    By explore in forum Java Networking
    Replies: 1
    Last Post: October 18th, 2010, 04:57 PM