Computer Science, asked by saniyadixit112, 17 days ago

write a program in java to store names of the 10 politicians and their party to which they belong to in two separate array. Ask the user to enter the name of the party. Printout all the name of the politicians who belong to that party. Repeated the process according to user choice. using linear search ​

Answers

Answered by samarthkrv
2

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

 String[] politicians = new String[10];

 String[] party = new String[10];

 System.out.println("Enter the names of all 10 politicians");

     for(int i = 0; i < politicians.length; i++){

         politicians[i] = sc.next();

     }

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

         System.out.print("Enter the name of the party to which " + politicians[i] + " belongs to:");

        party[i] = sc.next();

     }

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

         System.out.println(i + "). " + politicians[i] + "-" + party[i]);

     }

    System.out.print("Now enter the name of a party:");

    String partyName = sc.next();

    System.out.println("All politicians belonging to " + partyName + " are-");

        for(int i = 0; i < party.length; i++){

                if(party[i].equals(partyName)){

                    System.out.println(politicians[i]);

                }

        }

}

}

Explanation:

Similar questions