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...


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:


How many names do you want to enter? 5<br>Enter a name: Craig<br>Enter a name: Craig<br>Enter a name: Constance<br>Enter a name: Jonathan<br>Enter a name: Craig<br>which name are you looking for?<br>craig<br>Removing Craig from the array.<br>Removing Craig from the array.<br>Removing Craig from the array.<br>Here are the remaining names:<br>Constance Jonathan<br>How many names do you want to enter? 3<br>Enter a name: Craig<br>Enter a name: Serafina<br>Enter a name: Tobias<br>which name are you looking for?<br>David<br>Name not found.<br>Here are the remaining names:<br>Craig Serafina Tobias<br>

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
Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here