Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class called TestArrays1 to store and retrieve login information of 5 us

ID: 3716452 • Letter: W

Question

Write a class called TestArrays1 to store and retrieve login information of 5 users (JAVA).

In this class have a main method. In the main method, declare and create a 10 element String array called userid. Then create another 10 element String array called password. Using a for loop, ask the user to enter each userid and store this userid as an element of the useridArray. Then ask for the user’s password and store this password as an element of the passwordArray. Continue until you fill the arrays with the userid and password for each of the 5 users.

Once you fill the two arrays, you will check to see if the password and userid match. To do this, you will have the user enter a userid and then its password. Store these values in two variables. You will now search the arrays to see if these userid and password matches with what you have stored in the arrays. To do this you will first obtain the index of this provided userid by doing the following linear search, using a for loop:

/find index of given user id
for(int i = 0; i< useridArray.length; i= i+1)
{
if(useridArray[i].equals(searchValue))
n = i;
}

where useridArray is the name of the array and searchValue is the userid entered by the user. The linear-search return the index of the value/userid searched.

                    Use the value of the index, say n, of the userid and obtain the value of the nth element of the passwordArray. Check to see if this value matches with the password entered by the user. If the values match, tell the user that they have been authenticated. Or else, tell the user that the password in invalid.       

Explanation / Answer

import java.util.Scanner;

public class Driver {

public static void main(String[] args){

Scanner in = new Scanner(System.in);

String useridArray[] = new String[5];

String passwordArray[] = new String[5];

for(int i = 0; i < 5; i++) {

System.out.println("Enter user id for user " + (i +1));

useridArray[i] = in.nextLine();

System.out.println("Enter password for user " + (i +1));

passwordArray[i] = in.nextLine();

}

String searchValue, searchPassword;

System.out.println("Enter user id to search ");

searchValue = in.nextLine();

System.out.println("Enter password to search ");

searchPassword = in.nextLine();

int n = -1;

for(int i = 0; i< useridArray.length; i= i+1)

{

if(useridArray[i].equals(searchValue))

n = i;

break;

}

if(n == -1)

System.out.println("User id not found");

else {

if(passwordArray[n].equals(searchPassword))

System.out.println("Password correct");

else

System.out.println("Password incorrect");

}

}

}

**Comment for any further queries.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote