Java 过滤进入我的数组列表的内容
我正在编写一个程序,它读取一个.txt文件并将每个单词存储到一个数组中。到目前为止,我有这个。它的商店好Java 过滤进入我的数组列表的内容,java,arraylist,Java,Arraylist,我正在编写一个程序,它读取一个.txt文件并将每个单词存储到一个数组中。到目前为止,我有这个。它的商店好 BufferedReader fr = new BufferedReader(new FileReader(file)); ArrayList<String> words = new ArrayList<String>(50); String[] line; String str; whi
BufferedReader fr = new BufferedReader(new FileReader(file));
ArrayList<String> words = new ArrayList<String>(50);
String[] line;
String str;
while((str=fr.readLine()) != null)
{
line = str.split("(?s)\\s+");
for(String word : line)
words.add(word);
}
if(words.size() > 49){
System.out.println("To many words in file.");
System.exit(1);
}
for(String word : words){
if(word.length() > 12){
}
}
// Printing the content of words
for(String word : words)
System.out.println("sting entry:" + word);
}
为了检查并查看所有存储的内容,我得到了以下结果:
sting entry:it's
sting entry:yes-man
sting entry:murdered
sting entry:ok
sting entry:
sting entry:Hello
sting entry:Friend
这很好,只是它将返回值作为一个字读取并存储在数组中。我如何防止这种情况
当我在做的时候。。。如果您注意到我的代码中有一些随机代码:
for(String word : words){
if(word.length() > 12){
}
}
我想使
ArrayList
也不会存储超过12个字符的字符串值。我如何实现这一点呢?非常感谢advanced。在代码的这一点上,请执行以下操作:
for (String word : line)
{
word = word.trim();
if (word.length > 0 && word.length <= 12) { words.add(word); }
}
for(字符串字:行)
{
word=word.trim();
如果(word.length>0&&word.length,那么word.trim()在这里做什么?
for (String word : line)
{
word = word.trim();
if (word.length > 0 && word.length <= 12) { words.add(word); }
}