In this we will learn how to add n string in a Vector Array in java
Program to add n string in a Vector Array
WAP TO ADD N STRINGS IN A VECTOR ARRAY. INPUT NEW STRING S AND CHECK WHETHER IT PRESENT IN THE VECTOR. IF PRESENT DELETE IT OR ADD ANOTHER VECTOR.
Also, you can use the Java compiler to compile a program.
Source code
import java.util.*;
class vector
{
public static void main(String args[])
{
Vector c= new Vector();
Scanner s=new Scanner(System.in); // CREATE OBJECT
int n;
String name;
int choice;
do{
System.out.println("1.insert\n2.delete\n3.display\nEnter
ur choice");
choice=s.nextInt();
switch(choice)
{
case 1: for(int i=1;i<=3;i++)
{
System.out.println("Enter city");
name=s.next();
if(c.contains(name))
{
System.out.println("City is
already exist");
}
else
{
c.addElement(name);
}
}
break;
case 2: System.out.println("Enter
city");
name=s.next();
if(c.contains(name))
{
c.remove(name);
}
else
{
System.out.println("city
doesnot exist");
}
break;
case 3:
Enumeration nam =c.elements();
System.out.println("cities");
while(nam.hasMoreElements())
System.out.println(nam.nextElement()+" ");
System.out.println(" \n");
break;
}// END OF SWITCH
System.out.println("1.yes\n2.no\n");
n=s.nextInt();
}while(n==1);
}// END OF MAIN
} // END OF CLASS
Output.
C:\A>javac vector.java
C:\A>java vector
insert
delete
display
Enter ur choice
1
Enter city
UP
Enter city
MP
Enter city
MUMBAI
DO YOU WANT TO CONTINUE
yes
2 . no
1
1 . insert
delete
display
Enter ur choice
3
cities
UP
MP
MUMBAI
DO YOU WANT TO CONTINUE
1 .yes
2 . no
1insert
delete
display
Enter ur choice
2
Enter city
UP
DO YOU WANT TO CONTINUE
yes
no
1
insert
delete
display
Enter ur choice
3
cities
MP
MUMBAI
Also, View- Write a java program to create a window using swing.
Incredible points. Sound arguments. Keep up the great work.