This program is fine, runs perfectly. Just need a little help with a little modi
ID: 3641296 • Letter: T
Question
This program is fine, runs perfectly. Just need a little help with a little modification. How do i change this program so that it asks the user for index number for option 4&5 (when the user tries to add or remove a string). And how do i change it so that it already has 5 strings (juni, jimi, michael, matt, brian) in it, instead of asking the user for them......Thank You
A sample output'll be much appreciated.........
import java.util.*;
import java.util.Scanner;
public class MenuBasedList {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
List nameList = new ArrayList();
ListIterator 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
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 nameList = new ArrayList(); nameList.add("Juni"); nameList.add("Jimi"); nameList.add("michale"); nameList.add("matt"); nameList.add("brian"); ListIterator it = null; int numberOfStrings; int option = 0; while (option != 7) { System.out.println("______MENU______"); System.out.println("4.Insert a string."); System.out.println("5.Remove a string."); System.out.println("Please select an option from MENU."); option = keyboard.nextInt(); switch (option) { 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 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.