i need help with filling in/correct each line that is indicated by a comment wit
ID: 3932963 • Letter: I
Question
i need help with filling in/correct each line that is indicated by a comment with a number
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class SetTest1
{
private static Scanner scanner = new Scanner(System.in);
public static void main(String args[])
{
// #1 Declare a Hashset of Strings called names
getNames(names); // get input from user
searchNames(names); // search for names
}
// get names
public static void getNames(Set<String> names)
{
// get name from standard input
System.out.println("Add a name to set, use end to terminate input:");
String inputName = scanner.next();
// obtain input until end entered
while (!inputName.equals("end"))
{
// insert name
// #2 add names to the list
System.out.println("Add a name to set, use end to terminate input:");
inputName = scanner.next();
}
}
// search names from list
private static void searchNames(Set<String> names)
{
// get name from standard input
System.out.println(
"Search a name, use end to terminate searching:");
String inputName = scanner.next();
// obtain input until end entered
while (!inputName.equals("end"))
{
//#3 if the name is found, please print it, if not tell me it is not in the set
System.out.println("Search a name, use end to terminate searching:");
inputName = scanner.next();
}
}
} // end class ListTest
Explanation / Answer
Code is added in program made as BOLD
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class SetTest1
{
private static Scanner scanner = new Scanner(System.in);
public static void main(String args[])
{
// #1 Declare a Hashset of Strings called names
getNames(names); // get input from user
searchNames(names); // search for names
}
// get names
public static void getNames(Set<String> names)
{
// get name from standard input
System.out.println("Add a name to set, use end to terminate input:");
String inputName = scanner.next();
// obtain input until end entered
while (!inputName.equals("end"))
{
// insert name
// #2 add names to the list
names.add(inputName);
System.out.println("Add a name to set, use end to terminate input:");
inputName = scanner.next();
}
}
// search names from list
private static void searchNames(Set<String> names)
{
// get name from standard input
System.out.println(
"Search a name, use end to terminate searching:");
String inputName = scanner.next();
// obtain input until end entered
while (!inputName.equals("end"))
{
//#3 if the name is found, please print it, if not tell me it is not in the set
boolean result=names.contains(inputName);
if(result){
System.out.println("Search String Found "+inputName);
}
else{
System.out.println("Search String Not Found ");
}
System.out.println("Search a name, use end to terminate searching:");
inputName = scanner.next();
}
}
} // end class ListTest
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.