Question
hi! i need your help to use trim method to remove space for items and amount for the below question When submitting this lab, submit
hi! i need your help to use trim method to remove space for items and amount for the below question When submitting this lab, submit a .java file called ShoppingList2, and create the following structure in Eclipse: Package Name: week9 Class Name: ShoppingList2 For this lab you will be updating your last program to make the shopping list more interesting. This is about string methods. Instead of simply entering items into a shopping list, your users will now add items and the corresponding amounts desired into the shopping list as a single string using the format :. Your program will need to split the string to get the proper formatting as described below and shown in the example run. String formatting must appear exactly as shown in the example run. Make the following updates to your last program addItems() Require the user to use a : when entering new items into the shopping list with the following format: : BEFORE adding the user entry into your shoppingList ArrayList, do the following: Check to be sure that each new item includes a : If the item does not contain a :, display a helpful message indicating the required format as shown in the example run. Remove any leading or trailing spaces before and after the AND before and after the . Do this before adding the item to the shopping list. Example: If the user enters the string apples : 6 , then apples:6 should be added to the shopping list. showItems() Display your Shopping List to look like the following: -------------------------- Shopping List ---------------------------- Apples 6 Oranges 10 -------------------------- To get your shopping list to display as shown above, you will need to: Print the title for your Shopping List Split each item into its itemName and itemAmount Use System.out.printf() to print the itemName and itemAmount with width specified deleteItems() There are no changes to deleteItems(). sort_Items() There are no changes to sortItems(). Hint: When splitting a string into two substrings based on a given character delimiter, a combination of indexOf() and substring() can be used. Example: To separate Hello,World into two strings, Hello and World do the following: originalWord = Hello,World; //the original user input commaIndex = originalWord.indexOf(,); //gets the index number for where the , is located firstWord = originalWord.substring(0, commaIndex); //gets everything before the comma secondWord = originalWord.substring(commaIndex + 1); //gets everything after the comma Results: firstWord now equals "Hello" secondWord now equals "World" Example Run 1. Add Items 2. Delete Items 3. Show Items 4. Sort Items 5. Exit Please enter a command: 1 Add an item to the list (or just hit 'ENTER' when done): Fruit Invalid Entry. No ':' found. Entry must be in the form ':' Add an item to the list (or just hit 'ENTER' when done): Oranges : 12 'Oranges:12' has been added to the Shopping List. Add an item to the list (or just hit 'ENTER' when done): Breakfast Bars: 1 'Breakfast Bars:1' has been added to the Shopping List. Add an item to the list (or just hit 'ENTER' when done): Eggs : 24 'Eggs:24' has been added to the Shopping List. Add an item to the list (or just hit 'ENTER' when done): Apples:6 'Apples:6' has been added to the Shopping List. Add an item to the list (or just hit 'ENTER' when done): 4 items have been added to your Shopping List. 1. Add Items 2. Delete Items 3. Show Items 4. Sort Items 5. Exit Please enter a command: 3 --------------------------- Shopping List --------------------------- Oranges 12 Breakfast Bars 1 Eggs 24 Apples 6 --------------------------- 1. Add Items 2. Delete Items 3. Show Items 4. Sort Items 5. Exit Please enter a command: 4 The Shopping List has been sorted. --------------------------- Shopping List --------------------------- Apples 6 Breakfast Bars 1 Eggs 24 Oranges 12 --------------------------- 1. Add Items 2. Delete Items 3. Show Items 4. Sort Items 5. Exit Please enter a command: 5
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