Hi i need help with a code to removes all duplicates words and all words that ar
ID: 3547690 • Letter: H
Question
Hi i need help with a code to removes all duplicates words and all words that are less than 8 characters in length and remove any leading or trailing white space and also remove the last word . Example
input file:
about adv
about prep
above adv
above rep
absence n
absolutely adv
academic adj
accept n
access v
output file:
absolutely
academic
Explanation / Answer
import java.io.*;
import java.util.*;
public class SampleTest {
public static void main(String[] args) throws IOException {
Set<String> arr = new HashSet<String>();
Scanner inScanner = new Scanner(new File("input.txt"));
while (inScanner.hasNext()) {
String str = inScanner.nextLine();
String word= str.trim();
if(word.length() < 8) {
continue;
}
arr.add(word);
}
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("output.txt")));
for(int i=0;i<arr.size()-1;i++) {
writer.write(arr[i]+" ");
}
writer.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.