Question
Modify Java code below ???? rules: - program starts with asking the user how many items they want to add - user will be asked
Modify Java code below ????
rules:
- program starts with asking the user how many items they want to add
- user will be asked to enter in the items to fill the slots
-use functions, all should not be located in main
-let user keep searching using loop and terminate when user wants
-take the search for items in array out of main and create a separate function for it so a user can search items
6.search function return following to user:
item name if its found
no match if not located
import java.util.ArrayList;
import java.util.Scanner;
public class Inventory {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("How many items to add:");
int a = sc.nextInt();
ArrayList
for (int i = 0; i < a; i++) {
System.out.printf("Enter %d Item: ", i + 1);
String s = sc.next();
Items.add(s);
}
for (int i = 0; i < Items.size(); i++)
{
System.out.println(Items.get(i));
}
System.out.println("Name of Item to search: ");
String str = sc.next();
// calling isExist by passing the string and list
if(isExist(str, Items)){
System.out.println(str+" Found");
}
else{
System.out.println(str+" Not Found");
}
sc.close();
}
// method which is used to find the string in the arraylist
public static boolean isExist(String str,ArrayList
return items.contains(str);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started