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 Items = new ArrayList(a);

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,ArrayListitems){

return items.contains(str);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!