Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*******//I have the program, but for some reason option 3 works. None of the oth

ID: 3640097 • Letter: #

Question

*******//I have the program, but for some reason option 3 works. None of the other options work. And what do i have to do about option 4 and 5, and the try and catch constructs...****///////


Write a menu-based program (see menubased.java) that allows
a user to manipulate a list of strings. That is, you should do
the following in your "main" method:

- Create a list (object) of strings.

- Add an initial set of strings (say, 3 to 5 of them) to the
list that you created. You choose the actual strings. You
can hard-code these strings in the "add" statements that
you write.

- Start the menu-based interaction with a menu that provides
these options:

1. Show list
2. Show list in reverse order
3. Show size of list
4. Insert a string
5. Remove a string
6. Remove all strings
7. Quit

Note that for Option 4 you will need to ask the user for the
string to be added. Additionally, for Options 4 and 5 you will
need to ask the user for the position number (index).

For Options 4 and 5, protect your program against a
crash by including try/catch constructs.

import java.util.*;
import java.util.Scanner;

public class MenuBasedList
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
List<String> nameList = new ArrayList<String>();
ListIterator<String> it= nameList.listIterator();

int numberOfStrings;

System.out.println("How many srtings do you want to add to the list ? ");
numberOfStrings = keyboard.nextInt();

for(int i = 0; i < numberOfStrings; i++)
{
System.out.println("Enter name "+(i+1)+": ");
String name = keyboard.next();
nameList.add(name);
}

System.out.println("______MENU______");
System.out.println("1.Show list.");
System.out.println("2.Show list in reverse order.");
System.out.println("3.Show size of list");
System.out.println("4.Inset a string.");
System.out.println("5.Remove a string.");
System.out.println("6.Remove all strings.");
System.out.println("7.Quit.");
System.out.println("Please select an option from MENU.");

int option = keyboard.nextInt();
switch(option)
{
case 1:
{
System.out.println("The strings in the list are:");
while(it.hasNext())
{
System.out.println(it.next());
}
break;
}
case 2:
{
System.out.println("Reverse order of the list is:");
while(it.hasNext())
{
System.out.println(it.previous());
}
break;
}
case 3:
{
System.out.println("The size of list is: "+nameList.size());
break;
}
case 4:
{
System.out.println("Enter a string to insert into the list: ");
String s = keyboard.nextLine();
nameList.add(s);
break;
}
case 5:
{
it.remove();

break;
}
case 6:
{
while(it.hasNext())
{
it.remove();
}
break;
}
case 7:
{
System.exit(0);
break;
}
default :
System.out.println("Please 1 to 7 only");
}//end of switch statement
}//end of main method
}//end of class

Explanation / Answer


import java.util.*;
import java.util.Scanner;

public class MenuBasedList {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        List<String> nameList = new ArrayList<String>();
        ListIterator<String> it = null;

        int numberOfStrings;

        System.out.println("How many srtings do you want to add to the list ? ");
        numberOfStrings = keyboard.nextInt();

        for (int i = 0; i < numberOfStrings; i++) {
            System.out.println("Enter name " + (i + 1) + ": ");
            String name = keyboard.next();
            nameList.add(name);
        }
        int option = 0;
        while (option != 7) {
            System.out.println("______MENU______");
            System.out.println("1.Show list.");
            System.out.println("2.Show list in reverse order.");
            System.out.println("3.Show size of list");
            System.out.println("4.Insert a string.");
            System.out.println("5.Remove a string.");
            System.out.println("6.Remove all strings.");
            System.out.println("7.Quit.");
            System.out.println("Please select an option from MENU.");

            option = keyboard.nextInt();
            switch (option) {
                case 1: {
                    System.out.println("The strings in the list are:");
                    it = nameList.listIterator();
                    while (it.hasNext()) {
                        System.out.println(it.next());
                    }
                    break;
                }
                case 2: {
                    System.out.println("Reverse order of the list is:");
                    List reverseList = nameList;
                    java.util.Collections.reverse(reverseList);
                    it = reverseList.listIterator();
                    while (it.hasNext()) {
                        System.out.println(it.next());
                    }
                    reverseList = null;
                    break;
                }
                case 3: {
                    System.out.println("The size of list is: " + nameList.size());
                    break;
                }
                case 4: {
                    System.out.println("Enter a string to insert into the list: ");
                    try {
                        nameList.add(keyboard.next());
                    } catch (Exception e) {
                        System.out.println("Stopped from crashing");
                    }
                    break;
                }
                case 5: {
                    try {
                        System.out.println("Enter String you want to remove");
                        nameList.remove(keyboard.next());
                        System.out.println(" Update list after removing");
                        it = nameList.listIterator();
                        while (it.hasNext()) {
                            System.out.println(it.next());
                        }
                    } catch (Exception e) {
                        System.out.println("Stopped from crashing");
                    }
                    break;
                }
                case 6: {
                    nameList.clear();
                    break;
                }
                case 7: {
                    System.exit(0);
                    break;
                }
                default:
                    System.out.println("Please 1 to 7 only");
            }//end of switch statement
        }
    }//end of main method
}//end of class
import java.util.*;
import java.util.Scanner;

public class MenuBasedList {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        List<String> nameList = new ArrayList<String>();
        ListIterator<String> it = null;

        int numberOfStrings;

        System.out.println("How many srtings do you want to add to the list ? ");
        numberOfStrings = keyboard.nextInt();

        for (int i = 0; i < numberOfStrings; i++) {
            System.out.println("Enter name " + (i + 1) + ": ");
            String name = keyboard.next();
            nameList.add(name);
        }
        int option = 0;
        while (option != 7) {
            System.out.println("______MENU______");
            System.out.println("1.Show list.");
            System.out.println("2.Show list in reverse order.");
            System.out.println("3.Show size of list");
            System.out.println("4.Insert a string.");
            System.out.println("5.Remove a string.");
            System.out.println("6.Remove all strings.");
            System.out.println("7.Quit.");
            System.out.println("Please select an option from MENU.");

            option = keyboard.nextInt();
            switch (option) {
                case 1: {
                    System.out.println("The strings in the list are:");
                    it = nameList.listIterator();
                    while (it.hasNext()) {
                        System.out.println(it.next());
                    }
                    break;
                }
                case 2: {
                    System.out.println("Reverse order of the list is:");
                    List reverseList = nameList;
                    java.util.Collections.reverse(reverseList);
                    it = reverseList.listIterator();
                    while (it.hasNext()) {
                        System.out.println(it.next());
                    }
                    reverseList = null;
                    break;
                }
                case 3: {
                    System.out.println("The size of list is: " + nameList.size());
                    break;
                }
                case 4: {
                    System.out.println("Enter a string to insert into the list: ");
                    try {
                        nameList.add(keyboard.next());
                    } catch (Exception e) {
                        System.out.println("Stopped from crashing");
                    }
                    break;
                }
                case 5: {
                    try {
                        System.out.println("Enter String you want to remove");
                        nameList.remove(keyboard.next());
                        System.out.println(" Update list after removing");
                        it = nameList.listIterator();
                        while (it.hasNext()) {
                            System.out.println(it.next());
                        }
                    } catch (Exception e) {
                        System.out.println("Stopped from crashing");
                    }
                    break;
                }
                case 6: {
                    nameList.clear();
                    break;
                }
                case 7: {
                    System.exit(0);
                    break;
                }
                default:
                    System.out.println("Please 1 to 7 only");
            }//end of switch statement
        }
    }//end of main method
}//end of class