Write a java program to input name and mobile numbers of all employees of any office. Using "Linear Search", search array of phone numbers for a given "mobile number" and print name of employee if found, otherwise print a suitable message.
please somone answer it properl
Answers
Java program to find the employee name based on his phone number.
Program :
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args)
{
Scanner s = new Scanner(System.in);
int flag=-1;
System.out.print("Enter no. of employees present in the company : ");
int n = s.nextInt();
String[] namesarr = new String[n];
long[] phonearr = new long[n];
for(int i=0; i<n; i++)
{
System.out.print("Enter employee "+(i+1)+"'s name : ");
String name = s.nextLine();
System.out.print("Enter employee "+(i+1)+"'s phone number : ");
long phone = s.nextLong();
namesarr[i]=name;
phonearr[i]=phone;
}
System.out.print("Enter a phone number whose name should be printed : ");
long numbercheck=s.nextLong();
for(int j=0;j<n;j++)
{
if(phonearr[j]==numbercheck)
{
flag=j;
}
}
if(flag==-1)
{
System.out.print("There is no employee found with that phone number");
}
else
{
System.out.print("Employee whose phone number is "+numbercheck+", is "+namesarr[flag]);
}
}
}
Testcases :
Input 1 : (Number found case)
Enter no. of employees present in the company : 5
Enter employee 1's name : Raju
Enter employee 1's phone number : 9334567765
Enter employee 2's name : Vivek
Enter employee 2's phone number : 8345678987
Enter employee 3's name : Ravi
Enter employee 3's phone number : 9997652345
Enter employee 4's name : Arjun
Enter employee 4's phone number : 9876534567
Enter employee 5's name : Abhi
Enter employee 5's phone number : 8345633456
Enter a phone number whose name should be printed : 9876534567
Output 1 :
Employee whose phone number is 9876534567, is Arjun.
Input 2 : (Number NOT found case)
Enter no. of employees present in the company : 5
Enter employee 1's name : Raju
Enter employee 1's phone number : 9334567765
Enter employee 2's name : Vivek
Enter employee 2's phone number : 8345678987
Enter employee 3's name : Ravi
Enter employee 3's phone number : 9997652345
Enter employee 4's name : Arjun
Enter employee 4's phone number : 9876534567
Enter employee 5's name : Abhi
Enter employee 5's phone number : 8345633456
Enter a phone number whose name should be printed : 8678955677
Output 2 :
There is no employee found with that phone number.
Explanation :
- All you need to do here is, create two arrays of size n (no.of employees in the company).
- One array of type String that take Employee names and the other one of Long type, that takes the numbers.
- Read the employee names and numbers through one loop of range n, such the name present in index i of a string will parallely have it's number on index i of other array.
- Once the reading is completed, take a number from user whose name should be searched.
- Write a loop ranging n, and equate the phonenumberarray[iterable] with searching number.
- If match found, break the loop and print the name.
- If after searching throughout phonenumberarray and didn't find the number, print a not available message. That's it!
Learn more :
- Ever heard about methods in Java? About method overloading and overriding? If no, have a look at these links. These are one of the useful concepts of Java Programming.
brainly.in/question/1087167
brainly.in/question/314609