Please help with the following code so that ouput is same as sample: import java.io.*; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; import java.util.Set; public class main{ public static void main(String [] args) { Scanner s=new Scanner(System.in); String name; int n=0,flag=0; Set set=new HashSet(); do{ System.out.println("Enter the first name: "); name=s.next(); set.add(name); n++; }while(n!=3); System.out.println("Enter the name you want to search"); String search=s.next(); for(String element:set){ if(element.equals(search)){ System.out.println("Yes the name "+element+" is in the set"); flag=1; break; } } if(flag==0){ System.out.println("Yes the name "+search+" is not in the set"); } } }
Please help with the following code so that ouput is same as sample:
import java.io.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class main{
public static void main(String [] args) {
Scanner s=new Scanner(System.in);
String name;
int n=0,flag=0;
Set<String> set=new HashSet<String>();
do{
System.out.println("Enter the first name: ");
name=s.next();
set.add(name);
n++;
}while(n!=3);
System.out.println("Enter the name you want to search");
String search=s.next();
for(String element:set){
if(element.equals(search)){
System.out.println("Yes the name "+element+" is in the set");
flag=1;
break;
}
}
if(flag==0){
System.out.println("Yes the name "+search+" is not in the set");
}
}
}
Sample is given in attached images.
Step by step
Solved in 4 steps with 2 images