IT网络/编程学生在这里试图完成一项作业,我遇到了一个障碍。我们的任务是读取文本文件,将单词放入 ArrayList 中,并对内容执行字符串操作。我能够将单词拉入ArrayList,按升序对内容进行排序,删除任何少于四个字符的单词,删除重复的条目,并删除数字。然而,我发现带有撇号的单词正在被“切断”。像“不会”和“不能”这样的词被放入我的 ArrayList 中,作为“会”和“不能”。我已经为我的扫描仪对象尝试了不同的分隔符,但我似乎找不到一个可以在单词中保留撇号而不在撇号之后切断单词的分隔符。import java.io.File;import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.Collections;import java.util.LinkedHashSet;import java.util.Scanner;public class textFile { public static void main(String[] args) throws FileNotFoundException { // Scanner object reads in the required text file to the "words" ArrayList. Scanner sc = new Scanner(new File("textfile.txt"), "UTF-8"); ArrayList<String> words = new ArrayList<String>(); while (sc.hasNext()) { sc.useDelimiter("[^A-Za-z]"); words.add(sc.next().toLowerCase()); } // Closes the Scanner object used just above. sc.close(); // Sorts the "words" ArrayList in ascending order. Collections.sort(words); // Creates the "wordsNoDuplicates" ArrayList. Removes duplicate strings. LinkedHashSet<String> wordsNoDup = new LinkedHashSet<String>(words); // Removes all words containing less than four characters. wordsNoDup.removeIf(u -> u.length() < 4); // Prints the total number of words in the "wordsNoDup" ArrayList System.out.println("Total Number of Words: " + wordsNoDup.size() + "\n"); // Calculate and print the average word length. // double avgWordLength = 21186 / wordsNoDup.size(); System.out.println("Average Word Length: " + 7.0 + "\n"); // Print out the "words" ArrayList. Intended for debugging. System.out.print(wordsNoDup); System.out.println(); }}同样,像“不能”,“不应该”和“不会”这样的词被拉进来作为“可以”,“应该”和“会”。似乎是撇号和任何东西,它被丢弃。我会公开承认我不是一个对Java或编程有广泛了解的人,但任何帮助将不胜感激!
1 回答

慕的地6264312
TA贡献1817条经验 获得超6个赞
在代码中使用它,
sc.useDelimiter("[^A-Za-z]");
字母表以外的任何字符都将充当分隔符,因此也将充当分隔符,因此我建议将上面的代码行更改为此,'
sc.useDelimiter("[^A-Za-z']");
因此将不再被视为分隔符,并应保留在单词中。'
'
但我认为最好阅读你的文本并使用适当的正则表达式来匹配和过滤你的单词,所以,只有当它存在于单词中而不是可能在单词之外时,你才例外地允许a。'
添加回答
举报
0/150
提交
取消