Fix the Java code below, make sure it can make the same outputs as the sample:
import java.util.ArrayList;import java.util.Scanner;
class Main{ //Returns index of name if found otherwise -1 public static int findName(ArrayList arr, String name) { for(int i = 0; i < arr.size();=""> if(arr.get(i).equals(name)) return i; return -1;//Name not found } //Deletes a name from ArrayList public static void deleteName(ArrayList arr, String name) { int index = findName(arr, name);//Get index of name to be deleted for(int i = index; i < arr.size()="" -="" 1;=""> arr.set(i, arr.get(i + 1)); } arr.remove(arr.size() - 1);//Remove last element } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter number of students(upto 10): "); int n = scan.nextInt(); ArrayList arr = new ArrayList<>(n); for (int i = 0; i < n;="" i++)=""> System.out.print("Enter name of student " + (i+1) + ": "); arr.add(scan.next()); } System.out.print("Enter name of student to delete: "); String name = scan.next(); while(findName(arr, name) == -1)//Keep asking user for a valid name to delete { System.out.print("Name not found. Try a new name: "); name = scan.next(); } deleteName(arr, name);//Delete name from array System.out.println("Revised list of students: " + arr);//Print revised list }}Sample outputs in the picture:Extracted text: How many names do you want to enter? 5 Enter a name: Craig Enter a name: Craig Enter a name: Constance Enter a name: Jonathan Enter a name: Craig which name are you looking for? craig Removing Craig from the array. Removing Craig from the array. Removing Craig from the array. Here are the remaining names: Constance Jonathan How many names do you want to enter? 3 Enter a name: Craig Enter a name: Serafina Enter a name: Tobias which name are you looking for? David Name not found. Here are the remaining names: Craig Serafina Tobias
//Deletes a name from ArrayList public static void deleteName(ArrayList arr, String name) { int index = findName(arr, name);//Get index of name to be deleted for(int i = index; i < arr.size()="" -="" 1;=""> arr.set(i, arr.get(i + 1)); } arr.remove(arr.size() - 1);//Remove last element } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter number of students(upto 10): "); int n = scan.nextInt(); ArrayList arr = new ArrayList<>(n); for (int i = 0; i < n;="" i++)=""> System.out.print("Enter name of student " + (i+1) + ": "); arr.add(scan.next()); } System.out.print("Enter name of student to delete: "); String name = scan.next(); while(findName(arr, name) == -1)//Keep asking user for a valid name to delete { System.out.print("Name not found. Try a new name: "); name = scan.next(); } deleteName(arr, name);//Delete name from array System.out.println("Revised list of students: " + arr);//Print revised list }}Sample outputs in the picture:Extracted text: How many names do you want to enter? 5 Enter a name: Craig Enter a name: Craig Enter a name: Constance Enter a name: Jonathan Enter a name: Craig which name are you looking for? craig Removing Craig from the array. Removing Craig from the array. Removing Craig from the array. Here are the remaining names: Constance Jonathan How many names do you want to enter? 3 Enter a name: Craig Enter a name: Serafina Enter a name: Tobias which name are you looking for? David Name not found. Here are the remaining names: Craig Serafina Tobias
public static void main(String[] args) { Scanner scan = new Scanner(System.in);
System.out.print("Enter number of students(upto 10): "); int n = scan.nextInt();
ArrayList arr = new ArrayList<>(n); for (int i = 0; i < n;="" i++)=""> System.out.print("Enter name of student " + (i+1) + ": "); arr.add(scan.next()); } System.out.print("Enter name of student to delete: "); String name = scan.next(); while(findName(arr, name) == -1)//Keep asking user for a valid name to delete { System.out.print("Name not found. Try a new name: "); name = scan.next(); } deleteName(arr, name);//Delete name from array System.out.println("Revised list of students: " + arr);//Print revised list }}Sample outputs in the picture:Extracted text: How many names do you want to enter? 5 Enter a name: Craig Enter a name: Craig Enter a name: Constance Enter a name: Jonathan Enter a name: Craig which name are you looking for? craig Removing Craig from the array. Removing Craig from the array. Removing Craig from the array. Here are the remaining names: Constance Jonathan How many names do you want to enter? 3 Enter a name: Craig Enter a name: Serafina Enter a name: Tobias which name are you looking for? David Name not found. Here are the remaining names: Craig Serafina Tobias
for (int i = 0; i < n;="" i++)=""> System.out.print("Enter name of student " + (i+1) + ": "); arr.add(scan.next()); }
System.out.print("Enter name of student to delete: "); String name = scan.next();
while(findName(arr, name) == -1)//Keep asking user for a valid name to delete { System.out.print("Name not found. Try a new name: "); name = scan.next(); }
deleteName(arr, name);//Delete name from array
System.out.println("Revised list of students: " + arr);//Print revised list }}
Sample outputs in the picture:
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here