我的任务是从文本文档中检索最长的单词。我如何调整这一点,以适用于任何语言,如俄语或阿拉伯语。包含数字0-9的单词将被忽略,并在存储前删除单词中的任何标点符号。
例如。53
ex,§“-起诉-Ž,…美国国家统计局(Ž)的调查对象:(C)ŽŽŽŽ“Ž…‰§…”Ž数据
我的代码:
public Collection<String> getLongestWords() {
String longestWord = "";
String current;
Scanner scan = new Scanner(new File("file.txt"));
while (scan.hasNext()) {
current = scan.next();
if (current.length() > longestWord.length()) {
longestWord = current;
}
return longestWord;
}
}注意:我以前从未实现过unicode :/
发布于 2015-02-26 21:37:44
我相信:(找到并返回文本文件中最长的单词)
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class hello {
public static void main(String [ ] args) throws FileNotFoundException {
new hello().getLongestWords();
}
public String getLongestWords() throws FileNotFoundException {
String longestWord = "";
String current;
Scanner scan = new Scanner(new File("file.txt"));
while (scan.hasNext()) {
current = scan.next();
if (current.length() > longestWord.length()) {
longestWord = current;
}
}
System.out.println(longestWord);
return longestWord;
}
}带标点符号:
longestWord.replaceAll("[^a-zA-Z ]", "").split("\\s+");在你回来之前!
如果你不想用数字来考虑单词:
if ((current.length() > longestWord.length()) && (!current.matches(".*\\d.*"))) {一切都在一起:
import java.util.Scanner;
import java.io.*;
public class hello {
public static void main(String [ ] args) throws FileNotFoundException {
new hello().getLongestWords();
}
public String getLongestWords() throws FileNotFoundException {
String longestWord = "";
String current;
Scanner scan = new Scanner(new File("file.txt"));
while (scan.hasNext()) {
current = scan.next();
if ((current.length() > longestWord.length()) && (!current.matches(".*\\d.*"))) {
longestWord = current;
}
}
System.out.println(longestWord);
longestWord.replaceAll("[^a-zA-Z ]", "").split("\\s+");
return longestWord;
}
}https://stackoverflow.com/questions/28752858
复制相似问题