Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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<String> arr, String name)
    {
        for(int i = 0; i < arr.size(); i++)
            if(arr.get(i).equals(name))
                return i;
        return -1;//Name not found
    }

    //Deletes a name from ArrayList
    public static void deleteName(ArrayList<String> arr, String name)
    {
        int index = findName(arr, name);//Get index of name to be deleted
        for(int i = index; i < arr.size() - 1; i++){
            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<String> 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:

**Example Output of Name Search and Removal Program**

---

### Example 1

1. **Initial Input**: 
   - Number of names to enter: **5**
   - Entered names: Craig, Craig, Constance, Jonathan, Craig

2. **Name Search**:
   - Name being searched: **Craig**

3. **Process**:
   - Craig is found and removed from the list, leaving: Constance, Jonathan

4. **Output**:
   - Remaining names: **Constance, Jonathan**

---

### Example 2

1. **Initial Input**: 
   - Number of names to enter: **3**
   - Entered names: Craig, Serafina, Tobias

2. **Name Search**:
   - Name being searched: **David**

3. **Process**:
   - David is not found in the list.

4. **Output**:
   - Remaining names: **Craig, Serafina, Tobias**

--- 

This example demonstrates how input names can be managed by searching and removing specific entries, illustrating both successful and unsuccessful search scenarios.
expand button
Transcribed Image Text:**Example Output of Name Search and Removal Program** --- ### Example 1 1. **Initial Input**: - Number of names to enter: **5** - Entered names: Craig, Craig, Constance, Jonathan, Craig 2. **Name Search**: - Name being searched: **Craig** 3. **Process**: - Craig is found and removed from the list, leaving: Constance, Jonathan 4. **Output**: - Remaining names: **Constance, Jonathan** --- ### Example 2 1. **Initial Input**: - Number of names to enter: **3** - Entered names: Craig, Serafina, Tobias 2. **Name Search**: - Name being searched: **David** 3. **Process**: - David is not found in the list. 4. **Output**: - Remaining names: **Craig, Serafina, Tobias** --- This example demonstrates how input names can be managed by searching and removing specific entries, illustrating both successful and unsuccessful search scenarios.
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education