Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将文本文件中的唯一单词添加到ArrayList的程序_Java_Arraylist_Java.util.scanner - Fatal编程技术网

Java 将文本文件中的唯一单词添加到ArrayList的程序

Java 将文本文件中的唯一单词添加到ArrayList的程序,java,arraylist,java.util.scanner,Java,Arraylist,Java.util.scanner,我正在编写一个程序,它读取包含多行文本的文本文件,并向ArrayList添加唯一的单词。然后,我需要对这个ArrayList排序并打印它。 我的意见是: 你好,我的 我的名字是 爪哇 我最初认为我的问题是,一旦扫描仪击中ArrayList中已经存在的单词,它就会停止。但是,在改变我的输入后,我不知道我的问题是什么了。 我现在的输出是: 有两个唯一的单词 这些词是: 是 我的 我需要我的输出是: 有5个唯一的单词 这些词是: 你好 Java 是 我的 名称 这是我的密码: import java.

我正在编写一个程序,它读取包含多行文本的文本文件,并向ArrayList添加唯一的单词。然后,我需要对这个ArrayList排序并打印它。 我的意见是:

你好,我的
我的名字是
爪哇

我最初认为我的问题是,一旦扫描仪击中ArrayList中已经存在的单词,它就会停止。但是,在改变我的输入后,我不知道我的问题是什么了。 我现在的输出是:

有两个唯一的单词
这些词是:

我的

我需要我的输出是:

有5个唯一的单词
这些词是:
你好
Java

我的
名称

这是我的密码:

import java.util.*;
import java.io.*;
public class Indexer
{
   public static void main(String[] args) throws FileNotFoundException
   {
      Scanner fileScanner = new Scanner(new File("File.txt"));
      fileScanner.useDelimiter("[^A-Za-z0-9]");
      ArrayList<String> words = new ArrayList<String>();
      while (fileScanner.hasNext())
      { 
         if (!words.contains(fileScanner.next()) && (fileScanner.hasNext()))
         {
           words.add(fileScanner.next());
         }
      }
      Collections.sort(words);
      System.out.println("There are " +  words.size() + " unique word(s)");
      System.out.println("These words are:");
      for (Iterator<String> it = words.iterator(); it.hasNext();) 
      {
          String f = it.next();
          System.out.println(f);
      }
      fileScanner.close();
   }
}
import java.util.*;
导入java.io.*;
公共类索引器
{
公共静态void main(字符串[]args)引发FileNotFoundException
{
Scanner fileScanner=new Scanner(新文件(“File.txt”);
fileScanner.useDelimiter(“[^A-Za-z0-9]”);
ArrayList words=新的ArrayList();
while(fileScanner.hasNext())
{ 
如果(!words.contains(fileScanner.next())&&(fileScanner.hasNext())
{
add(fileScanner.next());
}
}
集合。排序(单词);
System.out.println(“有”+字.size()+唯一字)”;
System.out.println(“这些词是:”);
for(Iterator it=words.Iterator();it.hasNext();)
{
字符串f=it.next();
系统输出打印ln(f);
}
fileScanner.close();
}
}

我做错了什么?

首先,不要在循环中调用scanner.next()两次

  ArrayList<String> words = new ArrayList<String>();
  while (fileScanner.hasNext())
  { 
     String word = fileScanner.next();
     if (!words.contains(word))
     {
       words.add(word);
     }
  }
ArrayList words=new ArrayList();
while(fileScanner.hasNext())
{ 
String word=fileScanner.next();
如果(!words.contains(word))
{
添加(word);
}
}

首先,不要在循环中调用scanner.next()两次

  ArrayList<String> words = new ArrayList<String>();
  while (fileScanner.hasNext())
  { 
     String word = fileScanner.next();
     if (!words.contains(word))
     {
       words.add(word);
     }
  }
ArrayList words=new ArrayList();
while(fileScanner.hasNext())
{ 
String word=fileScanner.next();
如果(!words.contains(word))
{
添加(word);
}
}

首先,不要在循环中调用scanner.next()两次

  ArrayList<String> words = new ArrayList<String>();
  while (fileScanner.hasNext())
  { 
     String word = fileScanner.next();
     if (!words.contains(word))
     {
       words.add(word);
     }
  }
ArrayList words=new ArrayList();
while(fileScanner.hasNext())
{ 
String word=fileScanner.next();
如果(!words.contains(word))
{
添加(word);
}
}

首先,不要在循环中调用scanner.next()两次

  ArrayList<String> words = new ArrayList<String>();
  while (fileScanner.hasNext())
  { 
     String word = fileScanner.next();
     if (!words.contains(word))
     {
       words.add(word);
     }
  }
ArrayList words=new ArrayList();
while(fileScanner.hasNext())
{ 
String word=fileScanner.next();
如果(!words.contains(word))
{
添加(word);
}
}

您重复使用了
next()
,超出了应该使用的范围。收集变量中调用的
next()
的值,并使用该值。例如

while (fileScanner.hasNext())
    {
        String nextWord = fileScanner.next();
        if (!words.contains(nextWord))
        {
            words.add(nextWord);
        }
    }

您反复使用
next()
,超出了应该使用的范围。收集变量中调用的
next()
的值,并使用该值。例如

while (fileScanner.hasNext())
    {
        String nextWord = fileScanner.next();
        if (!words.contains(nextWord))
        {
            words.add(nextWord);
        }
    }

您反复使用
next()
,超出了应该使用的范围。收集变量中调用的
next()
的值,并使用该值。例如

while (fileScanner.hasNext())
    {
        String nextWord = fileScanner.next();
        if (!words.contains(nextWord))
        {
            words.add(nextWord);
        }
    }

您反复使用
next()
,超出了应该使用的范围。收集变量中调用的
next()
的值,并使用该值。例如

while (fileScanner.hasNext())
    {
        String nextWord = fileScanner.next();
        if (!words.contains(nextWord))
        {
            words.add(nextWord);
        }
    }
问题在于:

     if (!words.contains(fileScanner.next()) && (fileScanner.hasNext()))
     {
       words.add(fileScanner.next());
     }
每次调用
next()
都会读取另一个单词。因此,在上面的代码中,您正在阅读并测试一个单词,然后阅读并添加下一个单词。这是不对的。你不想扔掉那些话

提示:使用局部变量保存您刚刚读过的单词…

问题在于:

     if (!words.contains(fileScanner.next()) && (fileScanner.hasNext()))
     {
       words.add(fileScanner.next());
     }
每次调用
next()
都会读取另一个单词。因此,在上面的代码中,您正在阅读并测试一个单词,然后阅读并添加下一个单词。这是不对的。你不想扔掉那些话

提示:使用局部变量保存您刚刚读过的单词…

问题在于:

     if (!words.contains(fileScanner.next()) && (fileScanner.hasNext()))
     {
       words.add(fileScanner.next());
     }
每次调用
next()
都会读取另一个单词。因此,在上面的代码中,您正在阅读并测试一个单词,然后阅读并添加下一个单词。这是不对的。你不想扔掉那些话

提示:使用局部变量保存您刚刚读过的单词…

问题在于:

     if (!words.contains(fileScanner.next()) && (fileScanner.hasNext()))
     {
       words.add(fileScanner.next());
     }
每次调用
next()
都会读取另一个单词。因此,在上面的代码中,您正在阅读并测试一个单词,然后阅读并添加下一个单词。这是不对的。你不想扔掉那些话


提示:使用局部变量保存您刚刚读过的单词…

为什么不使用
,“一个不包含重复元素的集合。”为什么不在“”空格和新行上拆分字符串?@suncastOwner-OP当前的操作方式有什么问题?你的建议会使编程更复杂,而不是更少。使用集合可能会更简单。我不知道那就是布景。我们现在正在学校学习收集和基本数据结构。谢谢为什么不使用
集合
,“一个不包含重复元素的集合。”为什么不在“”空格和新行上拆分字符串?@SuncoastOwner-OP当前的操作方式有什么问题?你的建议会使编程更复杂,而不是更少。使用集合可能会更简单。我不知道那就是布景。我们现在正在学校学习收集和基本数据结构。谢谢为什么不使用
集合
,“一个不包含重复元素的集合。”为什么不在“”空格和新行上拆分字符串?@SuncoastOwner-OP当前的操作方式有什么问题?你的建议会使编程更复杂,而不是更少。使用集合可能会更简单。我不知道那就是布景。我们现在正在学校学习收集和基本数据结构。谢谢为什么?