Question

In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1.

import java.util.Scanner;

public class ShoppingList {

// Scanner that takes user input

static Scanner in = new Scanner(System.in);

public static String[] makeShoppingList(int size) {

// Initializing the array

String [] Groceries= new String[size];

for (int i=0; i<Groceries.length; i++) {

System.out.println("Enter an item: ");

// storing user input into the array

Groceries[i]=in.nextLine();

}

// return the array

return Groceries;

}

public static void main(String[]args) {

//not sure if this is what was supposed to be done but i could not figure out how to get a sentinel to determine size.

System.out.println("How many items are there?: ");

int size = in.nextInt();

in.nextLine();

String[]Groceries= makeShoppingList(size);

System.out.println("Your shopping list is: ");

//for loop for printing back out their shopping list

for (int i=0;i<Groceries.length;i++) {

System.out.println(Groceries[i]);

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class ShoppingList {
    // Scanner that takes user input
    static Scanner in = new Scanner(System.in);

    public static String[] makeShoppingList(int size) {
// Initializing the array
        String[] Groceries = new String[size];
        for (int i = 0; i < Groceries.length; i++) {
            System.out.println("Enter an item: ");
// storing user input into the array
            Groceries[i] = in.nextLine();
        }
// return the array
        return Groceries;
    }

    public static int findItem(String[] shoppingList, String item) {
        int index = -1;
        for (int i = 0; i < shoppingList.length; i++) {
            if (shoppingList[i].equals(item)) {
                index = i;
            }
        }
        if (index == -1) {
            System.out.println(item + " is not found in the grocery list");
        } else {
            System.out.println(item + " is found in the grocery list");
        }
        return index;
    }

    public static void main(String[] args) {
//not sure if this is what was supposed to be done but i could not figure out how to get a sentinel to determine size.
        System.out.println("How many items are there?: ");
        int size = in.nextInt();
        in.nextLine();
        String[] Groceries = makeShoppingList(size);
        System.out.println("Your shopping list is: ");
//for loop for printing back out their shopping list
        for (int i = 0; i < Groceries.length; i++) {
            System.out.println(Groceries[i]);
        }
        System.out.println("Enter an item to search for: ");
        String item = in.nextLine();
        int index = findItem(Groceries, item);
        if (index != -1) {
            System.out.println(item + " is found at index " + index);
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
In this same program I need to create a new method called “int findItem(String[] shoppingList, String...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ​I have to create two classes one class that accepts an object, stores the object in...

    ​I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList {    private ShoppingItem [] list;    private int amtItems = 0;       public ShoppingList()    {       list=new ShoppingItem[8];    }       public void add(ShoppingItem item)    {       if(amtItems<8)       {          list[amtItems] = ShoppingItem(item);...

  • Below is the code for the class shoppingList, I need to enhance it to accomplish the...

    Below is the code for the class shoppingList, I need to enhance it to accomplish the challenge level as stated in the description above. public class ShoppingList {   private java.util.Scanner scan; private String[] list; private int counter; public ShoppingList() { scan = new java.util.Scanner(System.in); list = new String[10]; counter = 0; } public boolean checkDuplicate(String item) { for(int i = 0; i < counter; i++) { if (list[i].equals(item)) return true; } return false; } public void printList() { System.out.println("Your shopping...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • You will need to think about problem solving. There are several multi-step activities. Design, compile and...

    You will need to think about problem solving. There are several multi-step activities. Design, compile and run a single Java program, StringEx.java, to accomplish all of the following tasks. Add one part at a time and test before trying the next one. The program can just include a main method, or it is neater to split things into separate methods (all static void, with names like showLength, sentenceType, lastFirst1, lastFirst), and have main call all the ones you have written...

  • Hi everyone! I need help on my Java assignment. I need to create a method that...

    Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...

  • Searching an array: --Using the template provided, create a method called search that takes an int...

    Searching an array: --Using the template provided, create a method called search that takes an int and an array as parameters. The method search should traverse the array to look for the int. search should print whether it found the int or not, for example: found: 10 did not find: 11 examples (bold face is text entered by user) % java SearchArray 20 found: 20 % java SearchArray 13 did not find: 13 % java SearchArray 100 found: 100 %...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • DESCRIPTION: You will be given a 2-D string array, called matrix. The array has an unknown...

    DESCRIPTION: You will be given a 2-D string array, called matrix. The array has an unknown number of cells filled with data. Your goal is to iterate through the 2-D array and keep a count of how many cells contain the string "Angela". INPUT: All input has been handled for you: A filled 2-D string array. PROCESSING: Determine the number of rows in the array matrix Determine the number of columns in the array matrix Use nested loops to iterate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT