Saturday, 31 October 2020

Write a program in Java to assign names of the 10 politicians and their party to which they belong to in two separate arrays. Ask the users to enter the name of the party. Printout the name of the politician who belong to that party. The program should be designed to give an error message when politician’s name is asked for a party whose name is not in list.

import java.util.Scanner;
public class party 
{
   public static void main(String[] args) 
   {
       
       Scanner sc = new Scanner(System.in);
       String[] w = { "Mamata Banerjee", "Kanshi Ram", "Syama Prasad Mukherjee","Jyoti Basu","P. A. Sangma","Sharad Pawar","Arvind Kejriwal","Debabrata Biswas","Asaduddin Owaisi","N. Rangaswamy"};
       String[] l = { "AITC", "BSP", "BJP","CPI","NPP","NCP","AAP","AIFB","AIMIM","AINRC"};
       System.out.print("Enter name of the party: ");
       String ch = sc.next();
       int index = -1;  
        for (int i = 0; i < l.length; i++) 
        {
        if (l[i].equals(ch)) 
        {
         index = i;
        }
       }
        if (index != -1) 
        {
            System.out.println(l[index] + " - " + w[index]);
        }  
        else 
        {
            System.out.println("Sorry Not Found!");
        }
    }
}



Output:
Enter name of the party: BJP
BJP - Syama Prasad Mukherjee
Enter name of the party: AAA
Sorry Not Found!

1 comment: