My code is the list of words in the file mywordst.txt is not being added into the array, and instead of going to the list. can someone help me correct it    public class MyArrayList2 {         public static void main(String[] args) {                 ArrayList aList = new ArrayList<>();                 Scanner fileReader;                 // here you can give the file name directly and make sure that the file is available in your project directory level                 fileReader = new Scanner(("myWords.txt"));                 Scanner keyboardReader = new Scanner(System.in);                 String inFilePath, line, word;                 int size = 0;                 while (fileReader.hasNext()) {                         line = fileReader.nextLine();                         if (line == null) {                                 break;                         }                         aList.add(line);                 } // while not end of file                 System.out.print("\n\nPlease enter the word you want to search for: ");                 word = keyboardReader.nextLine();                 if (aList.indexOf(word) >= 0) {                         System.out.println(word + " was found.\n\n");                 } else {                         System.out.println(word + " was not found.\n\n");                 }                 System.out.print("Please enter the word you want to remove: ");                 word = keyboardReader.nextLine();                 int removalCount = 0;                 while (aList.remove(word)) {                         removalCount++;                 }                 if (removalCount == 0) {                         System.out.println(word + " was not found, so not removed.\n\n");                 } else if (removalCount == 1) {                         System.out.println("The only instance of " + word + " was removed.\n\n");                 } else {                         System.out.println("All " + removalCount + " instances of " + word + " were removed.\n\n");                 }                 System.out.print("Please enter the word you want to append: ");                 word = keyboardReader.nextLine();                 aList.add(word);                 System.out.println(word + " was appended.\n\n");                 System.out.print("Please enter the word you want to upper case: ");                 word = keyboardReader.nextLine();                 int position = aList.indexOf(word);                 if (position >= 0) {                         aList.set(position, word.toUpperCase());                         System.out.println(word + " was converted to upper-case.\n\n");                 } else {                         System.out.println(word + " was not found, so not upper-cased.\n\n");                 }                 System.out.println("Here is the final version:\n" + aList);         } // method main }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

My code is the list of words in the file mywordst.txt is not being added into the array, and instead of going to the list. can someone help me correct it 

 

public class MyArrayList2 {

        public static void main(String[] args) {
                ArrayList<String> aList = new ArrayList<>();
                Scanner fileReader;
                // here you can give the file name directly and make sure that the file is available in your project directory level
                fileReader = new Scanner(("myWords.txt"));
                Scanner keyboardReader = new Scanner(System.in);
                String inFilePath, line, word;
                int size = 0;

                while (fileReader.hasNext()) {
                        line = fileReader.nextLine();
                        if (line == null) {
                                break;
                        }
                        aList.add(line);
                } // while not end of file

                System.out.print("\n\nPlease enter the word you want to search for: ");
                word = keyboardReader.nextLine();

                if (aList.indexOf(word) >= 0) {
                        System.out.println(word + " was found.\n\n");
                } else {
                        System.out.println(word + " was not found.\n\n");
                }
                System.out.print("Please enter the word you want to remove: ");
                word = keyboardReader.nextLine();

                int removalCount = 0;
                while (aList.remove(word)) {
                        removalCount++;
                }

                if (removalCount == 0) {
                        System.out.println(word + " was not found, so not removed.\n\n");
                } else if (removalCount == 1) {
                        System.out.println("The only instance of " + word + " was removed.\n\n");
                } else {
                        System.out.println("All " + removalCount + " instances of " + word + " were removed.\n\n");
                }

                System.out.print("Please enter the word you want to append: ");
                word = keyboardReader.nextLine();
                aList.add(word);
                System.out.println(word + " was appended.\n\n");
                System.out.print("Please enter the word you want to upper case: ");
                word = keyboardReader.nextLine();

                int position = aList.indexOf(word);
                if (position >= 0) {
                        aList.set(position, word.toUpperCase());
                        System.out.println(word + " was converted to upper-case.\n\n");
                } else {
                        System.out.println(word + " was not found, so not upper-cased.\n\n");
                }
                System.out.println("Here is the final version:\n" + aList);

        } // method main
}

Expert Solution
JAVA Program

import java.io.File;  
import java.io.FileNotFoundException; 
import java.util.Scanner;
import java.util.*; 

public class MyArrayList2 
{
    public static void main(String[] args) 
    {
        ArrayList<String> aList = new ArrayList<>();
        try
        {
        File myObj = new File("myWords.txt");
        Scanner sc = new Scanner(myObj);
        
        
        String inFilePath, line, word;
        int size = 0;

        while (sc.hasNextLine())
        {
            line = sc.nextLine();
            aList.add(line);
        } // while not end of file
        sc.close();
        
        Scanner sc1 = new Scanner(System.in);
        System.out.println("Here is the list: " + aList);
        System.out.print("\n\nPlease enter the word you want to search for: ");
        word = sc1.next();

        if (aList.indexOf(word) >= 0) 
        {
            System.out.println(word + " was found.\n\n");
        } 
        else
        {
            System.out.println(word + " was not found.\n\n");
        }
        
        System.out.print("Please enter the word you want to remove: ");
        word = sc1.next();

        int removalCount = 0;
        while (aList.remove(word)) 
        {
            removalCount++;
        }

        if (removalCount == 0) 
        {
            System.out.println(word + " was not found, so not removed.\n\n");
        } 
        else if (removalCount == 1) 
        {
            System.out.println("The only instance of " + word + " was removed.\n\n");
        }
        else 
        {
            System.out.println("All " + removalCount + " instances of " + word + " were removed.\n\n");
        }

        System.out.print("Please enter the word you want to append: ");
        word = sc1.next();
        aList.add(word);
        System.out.println(word + " was appended.\n\n");
                
        System.out.print("Please enter the word you want to upper case: ");
        word = sc1.next();

        int position = aList.indexOf(word);
        if (position >= 0) 
        {
            aList.set(position, word.toUpperCase());
            System.out.println(word + " was converted to upper-case.\n\n");
        } 
        else 
        {
            System.out.println(word + " was not found, so not upper-cased.\n\n");
        }
        System.out.println("Here is the final version:\n" + aList);
        
        }
        catch (FileNotFoundException e) 
        {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    } // method main
}

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY