Question

Create a java program that reads an input file named Friends.txt containing a list 4 names...

Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:  

1. Add a new name  

2. Change a name

3. Delete a name

4. Print the list  

or 5. Quit   

When the user selects Quit your app creates a new friends.txt output file.

Use methods: main( ) shouldn’t do all the work!

  

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.*;
import java.util.*;


class Driver {

    static void addName(List<String> names) {
        Scanner obj = new Scanner(System.in);
        System.out.println("enter new name to add");
        String name = obj.nextLine();
        names.add(name);
    }

    static void changeName(List<String> names) {
        Scanner obj = new Scanner(System.in);
        System.out.println("Enter name to change");
        String source = obj.nextLine();
        System.out.println("Enter name to replace");
        String target = obj.nextLine();
        int ind = names.indexOf(source);
        names.remove(source);
        names.add(ind, target);
    }

    static void deleteName(List<String> names){
        Scanner obj = new Scanner(System.in);
        System.out.println("Enter the name to delete");
        String del = obj.nextLine();
        names.remove(del);
    }

    public static void main(String[] args) throws FileNotFoundException {
        Scanner obj = new Scanner(System.in);
        Scanner f = new Scanner(new File("Friends.txt"));
        ArrayList<String> names = new ArrayList<String>();
        while (f.hasNextLine()) {
            names.add(f.nextLine());
        }
        System.out.println("1.Add a new name\n2.Change a name\n3.Delete a name\n4.Print the list\n5.Quit");
        int ch = Integer.parseInt(obj.nextLine());
        while (ch != 5) {

            if (ch == 1) {
                addName(names);
            } else if (ch == 2) {
                changeName(names);
            } else if (ch == 3) {
                deleteName(names);
            } else if (ch == 4) {
                System.out.println("List is " + names);
            }
            System.out.println("1.Add a new name\n2.Change a name\n3.Delete a name\n4.Print the list\n5.Quit");
            ch = Integer.parseInt(obj.nextLine());
        }

    }
}

OUTPUT :

1.Add a new name
2.Change a name
3.Delete a name
4.Print the list
5.Quit
1
enter new name to add
adam
1.Add a new name
2.Change a name
3.Delete a name
4.Print the list
5.Quit
4
List is [john, joseph, jose, sam, adam]
1.Add a new name
2.Change a name
3.Delete a name
4.Print the list
5.Quit
2
Enter name to change
sam
Enter name to replace
joseph
1.Add a new name
2.Change a name
3.Delete a name
4.Print the list
5.Quit
4
List is [john, joseph, jose, joseph, adam]
1.Add a new name
2.Change a name
3.Delete a name
4.Print the list
5.Quit
3
Enter the name to delete
joseph
1.Add a new name
2.Change a name
3.Delete a name
4.Print the list
5.Quit
5

Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Create a java program that reads an input file named Friends.txt containing a list 4 names...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Java program application that creates an ArrayList that holds String objects. It needs to...

    Create a Java program application that creates an ArrayList that holds String objects. It needs to prompt the user to enter the names of friends, which are then put into the ArrayList; allow the user to keep adding names until they enter "q" to quit. After input is complete, display a numbered list of all friends in the list and prompt the user to enter the number of the friend they wish to delete. After deleting that entry, output the...

  • Using Java Create an application that creates and displays a list of names using an array....

    Using Java Create an application that creates and displays a list of names using an array. Then modify the application to create and use a list of numbers. Console for the names application Please enter a name or type “exit” to quit: Diane Please enter a name or type “exit” to quit: Joe Please enter a name or type “exit” to quit: Greta Please enter a name or type “exit” to quit: Henry Please enter a name or type “exit”...

  • in java Complete Program that simulates a login procedure. The program reads a list of names,...

    in java Complete Program that simulates a login procedure. The program reads a list of names, email addresses and passwords from a file p3.txt. Store the information in parallel array lists names, emails and passwords. Your program will prompt the user for their email address. If the email is not in the system, prompt the user to try again. Provide the option to quit. If the email is found in the system, prompt the user to enter their password. After...

  • Write a java program that reads a file (names as textfile) uploaded with assignment and displays...

    Write a java program that reads a file (names as textfile) uploaded with assignment and displays the words of that file as a list. Then display the words in reverse order. Then display them with all plurals (ending in "s") capitalized. Then display them with all plural words removed.

  • Objective: Read in the names of several grocery items from the keyboard and create a shopping...

    Objective: Read in the names of several grocery items from the keyboard and create a shopping list using the Java ArrayList abstract data type. 1) Create a new empty ArrayList 2) Ask the user for 5 items to add to a shopping list and add them to the ArrayList (get from user via the keyboard). 3) Prompt the user for an item to search for on the list. Output a message to the user letting them know whether the item...

  • How can I create Loops and Files on Java? • Create a program that reads a...

    How can I create Loops and Files on Java? • Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...

  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • Create a Java file named Ch6Asg.java that contains a public class named Ch6Asg containing a main()...

    Create a Java file named Ch6Asg.java that contains a public class named Ch6Asg containing a main() method. Within the same file, create another class named Employee, and do not declare it public. Create a field for each of the following pieces of information: employee name, employee number, hourly pay rate, and overtime rate. Create accessor and mutator methods for each field. The mutator method for the hourly pay rate should require the value to be greater than zero. If it...

  • java Part 1 Create a NetBeans project that asks for a file name. The file should...

    java Part 1 Create a NetBeans project that asks for a file name. The file should contain an unknown quantity of double numeric values with each number on its own line. There should be no empty lines. Open the file and read the numbers. Print the sum, average, and the count of the numbers. Be sure to label the outputs very clearly. Read the file values as Strings and use Double.parseDouble() to convert them. Part 2 Create a NetBeans project...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT