Question: Stuck at one part of finding duplicates in a java program. My getOverLap(), is my problem. Thanks for any advice! 1 /******************************* 2 * Author:

Stuck at one part of finding duplicates in a java program. My getOverLap(), is my problem.

Thanks for any advice!

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.HashMap; 21 import java.util.Map; 22 import java.util.Scanner; 23 import java.io.FileNotFoundException; 24 import java.io.File; 25 26 public class a2_scan_for_duplicates{ 27 28 public static void main(String []args) 29 throws FileNotFoundException { 30 31 //hard-coded filename 32 //Scanner in1 = new Scanner(new File("words.txt")); 33 Scanner console = new Scanner(System.in); 34 35 //Prompt user for file name 36 System.out.println("Let's clean this up!"); 37 System.out.println("What's your file name? "); 38 Scanner in1 = new Scanner(new File(console.nextLine())); 39 System.out.println(); 40 41 //Scan words in file and store in a list 42 ArrayList list = getWords(in1); 43 44 //Find overlap within list 45 ArrayList common = getOverlap(list); 46 47 // TODO Auto-generated method stub 48 return; 49 50 }//end main method 51 52 53 /* 54 * This method reads words from scanner, converts 55 * them to lower case for sorting, and finds any 56 * duplicates within the file. 57 */ 58 public static ArrayList getWords(Scanner input) { 59 60 //Delimiter to ignore illegal characters 61 input.useDelimiter("[^a-zA-Z']+"); 62 63 //Read and sort words 64 ArrayList words = new ArrayList(); 65 66 while (input.hasNext()){ 67 String next = input.next().toLowerCase(); 68 words.add(next); 69 }//end while 70 71 Collections.sort(words); 72 73 //Look for unique words and keep them in a list 74 ArrayList result = new ArrayList(); 75 if (words.size() > 0) { 76 result.add(words.get(0)); 77 78 for (int i = 1; i < words.size(); i++){ 79 if (!words.get(i).equals(words.get(i-1))) { 80 result.add(words.get(i)); 81 }//end inner if 82 83 }//end for loop 84 85 }//end outter if 86 87 return result; 88 //System.out.println("All cleaned up! Here's your list with no duplicates: " + result); 89 90 }//end getWords method 91 92 /* 93 * This method will compare words within the text file 94 * and store any that are similar (or overlap) 95 * 96 * Condition: only accept letters 97 */ 98 public static ArrayList getOverlap( 99 ArrayList list){ 100 101 //remove non-character items like white spaces 102 //nStr = nStr.replace("_",""); 103 104 //Use a hashmap to track occurrences 105 Map occurrences = new HashMap(); 106 107 //perform iteration 108 for(String word: list){ 109 110 //check for existance in has. If null, reset count=0 111 Integer oldCount = occurrences.get(word); 112 113 if (oldCount == null){ 114 oldCount = 0; 115 116 //if !null, count occurence 117 occurrences.put(word, oldCount +1); 118 119 }//end if 120 }//end for loop 121 122 ArrayList overlapped = new ArrayList(); 123 124 for (String word : occurrences.keySet){ 125 126 Interger count = occurrences.get(word); 127 128 if(count > 1) 129 overlapped.add(word); 130 131 }//end if 132 133 return overlapped; 134 135 //end for 136 137 }//end getOverlap method 138 139 140 /* 141 * This method displays the ArrayList with all duplicate words 142 * that were removed. 143 */ 144 145 public static void duplicatesRemoved(ArrayList list, 146 ArrayList common) { 147 148 System.out.println("All cleaned up! Here's your list with no duplicates: " 149 + common); 150 151 }//end duplicatesRemoved method 152 153 }//end class 154

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!