its java programming. Exercise B: Write a program, MatchingArray , that contains
ID: 3931448 • Letter: I
Question
its java programming.
Exercise B:
Write a program, MatchingArray, that contains an array of five names and another array of five passwords(your choice). Your program should ask the user to enter a name and password. If the user enters the correct name with it's related password , Printout ..It's a match. If the user enters the wrong name/password (can be either or both), Print the message..It's not a match.
Note: In your array, first name should match with your first password. Second name should match with your second password...etc..
Explanation / Answer
/*this program has been tested on eclipse */
import java.util.Scanner; //for using scanner
public class HelloWorld{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
String usrname,pwd; //variable for storing the username and password //provided by the user
String name_ary[]={"jhon","ram","anurag","gopal","christina"}; //initializing a name array containing 5 //names of my choice,you can change the names
String password_ary[]={"#hello","1234","wifi12$","forgetpassword","12#abc$%"}; //initializing a password //array containing 5 passwords of my //choice,you can change the passwords
System.out.println("Enter a name "); //Asking the user to enter the username
usrname=sc.next(); //storing the user entered username in the //variable usrname
System.out.println("Enter a password "); //Asking the user to enter the password
pwd=sc.next(); //storing the user entered password in the //variable pwd
int index=-1; //initializing a variable index
for(int i=0;i<name_ary.length;i++) //searching the user entered usrname in the //name_ary
{
if(usrname.equals(name_ary[i])) //if user entered usrname is available in //name_ary
{
index=i; //store the index of the found username in index
break;
}
}
if( index >= 0 && index < name_ary.length ) //if index is greater than 0 and less than // 5,it means user entered username is available in //name_ary
{
if(password_ary[index].equals(pwd)) //find the user entered password in //password_ary at the index
System.out.println("It's a match "); //prints its a match
else
System.out.println("It's not a match "); //prints its not a match
}
else
System.out.println("It's not a match "); //prints its not a match
}
}
//end of program
**************OUTPUT***********
Enter a name
anurag
Enter a password
wifi12$
It's a match
**************OUTPUT***********
Please let me know in case of any question,thanks.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.