Write a java program which reads a series of first names & stores them in linked list. The program should not allow duplicate names and it should also allow the user to search first name.
import java.util.*; import java.io.*; class TestFirstName { public static void main(String[] args) { int n; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); LinkedList a1=new LinkedList(); try { System.out.print("How many name you want to accept:"); n=Integer.parseInt(br.readLine()); for(int j=0;j { String nm; System.out.print("Enter Name "+(j+1)+":"); nm=br.readLine(); Iterator it =a1.iterator(); int f=1; while(it.hasNext()) { if(nm.equals(it.next())) { System.out.println("No Duplicates Allowed...."); j--; f=0; break; } else { f=1; } } if(f==1) a1.add(nm); } } catch (Exception e) { System.out.println(e); } Iterator it=a1.iterator(); System.out.print("Enter name to Search:"); String nm=""; try{ nm=br.readLine(); } catch (Exception e) { System.out.println(e); } int f=1; while(it.hasNext()) { if(nm.equals(it.next())) { f=0; System.out.println("Record Found...."); break; } else { f=1; } } if(f==1) { System.out.println("Record Not Found...."); } } }
Write a java program which reads a series of first names & stores them in linked list. The program should not allow duplicate names and it should also allow the user to search first name.
Reviewed by
on
April 27, 2015
Rating: