Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Stuck at one part of finding duplicates in a java program. ~Line 95 within my getOverLap(), I'm stuck with combing through. I know I need

Stuck at one part of finding duplicates in a java program. ~Line 95 within my getOverLap(), I'm stuck with combing through. I know I need to do a nested j++ within the i but I'm stuck. I know this is an arcaic way of solving this but we're confined to the material we've only covered so far. Not sure how to attach the word.txt file here but my code is below.

Thanks for any advice!

*********************************

Program requirements:

Write a program to:

1) input values from a file called words.txt. The file consists of multiple records of multiple words.

2) read all of the words into one ArrayList

3) interrogate the ArrayList and remove all duplicate words.

4) display the ArrayList with all duplicate words removed.

*********************************

1 /******************************* 2 * Author: Danielle Coulter 3 * Title: a2_scan_for_duplicates.java 4 * Description: ArrayLists and duplicates 5 * Material: Chapter 10 6 * Date: 10/15/2017 7 * 8 * 9 * This program is designed to accept input from one .txt file, 10 * arrange the words into an array, and then scan for duplicates. 11 * 12 * Duplicates will be removed and then displayed on the console. 13 * 14 * @author Danielle Coulter 15 ****************************** 16 */ 17 18 import java.util.ArrayList; 19 import java.util.Collections; 20 import java.util.Scanner; 21 import java.io.FileNotFoundException; 22 import java.io.File; 23 24 public class a2_scan_for_duplicates{ 25 26 public static void main(String []args) 27 throws FileNotFoundException { 28 29 //hard-coded filename 30 //Scanner in1 = new Scanner(new File("words.txt")); 31 Scanner console = new Scanner(System.in); 32 33 //Prompt user for file name 34 System.out.println("Let's clean this up!"); 35 System.out.println("What's your file name? "); 36 Scanner in1 = new Scanner(new File(console.nextLine())); 37 System.out.println(); 38 39 //Scan words in file and store in a list 40 ArrayList list = getWords(in1); 41 42 //Find overlap within list 43 ArrayList common = getOverlap(list); 44 45 // TODO Auto-generated method stub 46 return result; 47 48 }//end main method 49 50 51 /* 52 * This method reads words from scanner, converts 53 * them to lower case for sorting, and finds any 54 * duplicates within the file. 55 */ 56 public static ArrayList getWords(Scanner input) { 57 58 //Delimiter to ignore illegal characters 59 input.useDelimiter("[^a-zA-Z']+"); 60 61 //Read and sort words 62 ArrayList words = new ArrayList(); 63 64 while (input.hasNext()){ 65 String next = input.next().toLowerCase(); 66 words.add(next); 67 }//end while 68 69 Collections.sort(words); 70 71 //Look for unique words and keep them in a list 72 ArrayList result = new ArrayList(); 73 if (words.size() > 0) { 74 result.add(words.get(0)); 75 76 for (int i = 1; i < words.size(); i++){ 77 if (!words.get(i).equals(words.get(i-1))) { 78 result.add(words.get(i)); 79 }//end inner if 80 81 }//end for loop 82 83 }//end outter if 84 85 return result; 86 //System.out.println("All cleaned up! Here's your list with no duplicates: " + result); 87 88 }//end getWords method 89 90 /* 91 * This method will compare words within the text file 92 * and store any that are similar (or overlap) 93 * 94 */ 95 public static ArrayList getOverlap( 96 ArrayList list){ 97 int i1 = 0; 98 while (i1 < list.geti1 99 100 }//end getOverlap method 101 102 /* 103 * This method displays the ArrayList with all duplicate words 104 * that were removed. 105 */ 106 107 public static void duplicatesRemoved(ArrayList list, 108 ArrayList common) { 109 110 System.out.println("All cleaned up! Here's your list with no duplicates: " 111 + common); 112 113 }//end duplicatesRemoved method 114 115 }//end class 116

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago