Write a program which asks the user to submit personal information and then by u
ID: 3545218 • Letter: W
Question
Write a program which asks the user to submit personal information and then by using methods to check them.
-user id
-password
-re enter password
-email adress
-name
-email address
-city
-state
-zipcode
-tel number
Write each of thes methods in your class.
1.) Method checking that no strings in the array are empty. If one is empty, return false, otherwise return true.
2.) Method returning the number of characters in user id.
3.) Method checking if the two Strings representing the passwords are identical. If they are, it returns true; if not, it returns false.
4.) Method checking if the String representing the email actually containes @ character and contains one or more periods after the @ character . If it does then the method returns true; otherwise, it return false.
Write a client class to test all these methods in your class.
Write comments to help me understand the code. Thank you.
Explanation / Answer
import java.util.*;
class ClientClass{
//Method to return length of the userid, lenght is an int so it is defined as int type
public int uidLength(String inUid){
String userId = inUid;
return userId.length();
}
// checks and retruns false if any input is empty else true
public boolean checkLengthAll(String[] a){
int j=0;
//if length of any of the input is 0, it increase the value of j by 1
for(int i=0;i<9;i++){
if(a[i].length() == 0){
j++;
}
i++;
}
// if the j is not 0 (i.e increased by the above check) then it returns false else true
if(j>0)
return false;
else
return true;
}
//checks if both passwords are same, returns true else false
public boolean pwdMatchCheck(String passwd1,String passwd2){
//equals() method check for the content of the string
//where as == check for the address of the string
if(passwd1.equals(passwd2))
return true;
else
return false;
}
//e-mail validation
public boolean validateEmail(String mailid){
//The below is the regular expression for checking the e-mail string--- this is common for all
String EMAIL_REGEX = "^[\w-_\.+]*[\w-_\.]\@([\w]+\.)+[\w]+[\w]$";
String email1 = mailid;
//matches method in string class matches the string with the given regular expession
//it returns true if the string matches with the regular exp else false
Boolean b = email1.matches(EMAIL_REGEX);
return b;
}
public static void main(String args[]){
//Below variables are to take the inputs from the user
String uid,pwd,repwd,name,email,city,state,zipcode,phone;
//This array is to save all the user inputs - as per your request
String[] arrayAll = new String[9];
//Scanner is used to read the user inputs from the console
Scanner s = new Scanner(System.in);
//Below lines of code is to take the inputs from the users
System.out.println("Enter username:");
uid = s.nextLine();
System.out.println("Enter password:");
pwd = s.nextLine();
System.out.println("Enter re-password:");
repwd = s.nextLine();
System.out.println("Enter Name:");
name = s.nextLine();
System.out.println("Enter email:");
email = s.nextLine();
System.out.println("Enter City:");
city= s.nextLine();
System.out.println("Enter State:");
state= s.nextLine();
System.out.println("Enter zipcode:");
zipcode = s.nextLine();
System.out.println("Enter phone:");
phone = s.nextLine();
//Below code is to initialize the array with the user inputs
arrayAll[0] = uid;
arrayAll[1] = pwd;
arrayAll[2] = repwd;
arrayAll[3] = name;
arrayAll[4] = email;
arrayAll[5] = city;
arrayAll[6] = state;
arrayAll[7] = zipcode;
arrayAll[8] = phone;
//Create an object for your class
ClientClass cc = new ClientClass();
//get the length of the userid using the function
int uidCount = cc.uidLength(uid);
System.out.println("Length of the userid : "+uidCount);
//check if any of the inputs are empty
boolean isEmptyCheck = cc.checkLengthAll(arrayAll);
System.out.println("Return False if anyone of the input is Empty : "+isEmptyCheck);
//check if password are identical
boolean arePwdSame = cc.pwdMatchCheck(pwd,repwd);
System.out.println("Return True if both passwords are same : "+arePwdSame);
//check if the user has entered correct e-mail
boolean emailCheck = cc.validateEmail(email);
System.out.println("Returns True if the e-mail is valid : "+emailCheck);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.