Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.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 从文本文件中读取单词和数字_Java_Java.util.scanner_Java Io - Fatal编程技术网

Java 从文本文件中读取单词和数字

Java 从文本文件中读取单词和数字,java,java.util.scanner,java-io,Java,Java.util.scanner,Java Io,我正在编写一个程序,读取一个文本文件,并将唯一的单词和数字添加到ArrayList中。我使用了一个分隔符,但是当我运行程序时,我得到了一个NoTouchElementException。是我的分隔符错了还是我又犯了一个错误 这是我的节目: import java.util.*; import java.io.*; public class Indexer { public static void main(String[] args) throws FileNotFoundExceptio

我正在编写一个程序,读取一个文本文件,并将唯一的单词和数字添加到ArrayList中。我使用了一个分隔符,但是当我运行程序时,我得到了一个NoTouchElementException。是我的分隔符错了还是我又犯了一个错误

这是我的节目:

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")).useDelimiter("[.,:;()?!\" \t]+~\\s");
      int totalWordCount = 0;
      ArrayList<String> words = new ArrayList<String>();
      while ((fileScanner.hasNext()) && (!words.contains(fileScanner.next())))
      {
         words.add(fileScanner.next());
         totalWordCount++;
      }
      System.out.println("There are " + totalWordCount + " unique word(s)");
      System.out.println("These words are:");
      System.out.println(words.toString());
      fileScanner.close();
    }
}    
import java.util.*;
导入java.io.*;
公共类索引器
{
公共静态void main(字符串[]args)引发FileNotFoundException
{
Scanner fileScanner=new Scanner(新文件(“File.txt”)。使用分隔符(“[,:;()?!\”\t]+~\\s”);
int totalWordCount=0;
ArrayList words=新的ArrayList();
而((fileScanner.hasNext())&&(!words.contains(fileScanner.next()))
{
add(fileScanner.next());
totalWordCount++;
}
System.out.println(“有”+totalWordCount+“唯一单词”);
System.out.println(“这些词是:”);
System.out.println(words.toString());
fileScanner.close();
}
}    

我会使用Set而不是List

Set<String> words = new HashSet<String>();
while (fileScanner.hasNext()) { 
      words.add(fileScanner.next());
Set words=newhashset();
而(fileScanner.hasNext()){
add(fileScanner.next());

我会使用Set而不是List

Set<String> words = new HashSet<String>();
while (fileScanner.hasNext()) { 
      words.add(fileScanner.next());
Set words=newhashset();
而(fileScanner.hasNext()){
add(fileScanner.next());

这应该可以工作,您可以使用tostring或迭代器来显示单词:

Set<String> words = new HashSet<String>();
      while ((fileScanner.hasNext())) { 
               words.add(fileScanner.next());
      }
      System.out.println("There are " +  words.size() + " unique word(s)");
      System.out.println("These words are:");
      //System.out.println(words.toString());
      for (Iterator<String> it = words.iterator(); it.hasNext(); ) {
          String f = it.next();
          System.out.println(f);
      }
      fileScanner.close();
Set words=newhashset();
而((fileScanner.hasNext()){
add(fileScanner.next());
}
System.out.println(“有”+字.size()+唯一字)”;
System.out.println(“这些词是:”);
//System.out.println(words.toString());
for(Iterator it=words.Iterator();it.hasNext();){
字符串f=it.next();
系统输出打印ln(f);
}
fileScanner.close();

这应该可以工作,您可以使用tostring或迭代器来显示单词:

Set<String> words = new HashSet<String>();
      while ((fileScanner.hasNext())) { 
               words.add(fileScanner.next());
      }
      System.out.println("There are " +  words.size() + " unique word(s)");
      System.out.println("These words are:");
      //System.out.println(words.toString());
      for (Iterator<String> it = words.iterator(); it.hasNext(); ) {
          String f = it.next();
          System.out.println(f);
      }
      fileScanner.close();
Set words=newhashset();
而((fileScanner.hasNext()){
add(fileScanner.next());
}
System.out.println(“有”+字.size()+唯一字)”;
System.out.println(“这些词是:”);
//System.out.println(words.toString());
for(Iterator it=words.Iterator();it.hasNext();){
字符串f=it.next();
系统输出打印ln(f);
}
fileScanner.close();

NoSuchElementException可能来自while循环中的第二个fileScanner.next()

当到达文件中的最后一个元素时,将在while循环条件下从fileScanner.next()读取该元素,从而导致在循环中进行第二次fileScanner调用时,没有剩余的元素

一种解决方案是每次迭代调用fileScanner.next()一次:

  Scanner fileScanner = new Scanner(new File("File.txt")).useDelimiter("[.,:;()?!\" \t]+~\\s");
  int totalWordCount = 0;
  Set<String> words = new HashSet<String>();
  String nextWord;
  while ((fileScanner.hasNext()) && (!words.contains(nextWord = fileScanner.next())))
  {
     words.add(nextWord);
     totalWordCount++;
  }
  System.out.println("There are " + totalWordCount + " unique word(s)");
  System.out.println("These words are:");
  System.out.println(words.toString());
  fileScanner.close();
}
Scanner fileScanner=new Scanner(新文件(“File.txt”)。使用分隔符(“[,:;()?!\”\t]+~\\s”);
int totalWordCount=0;
Set words=新HashSet();
字符串nextWord;
而((fileScanner.hasNext())&&(!words.contains(nextWord=fileScanner.next()))
{
单词。添加(nextWord);
totalWordCount++;
}
System.out.println(“有”+totalWordCount+“唯一单词”);
System.out.println(“这些词是:”);
System.out.println(words.toString());
fileScanner.close();
}

NoSuchElementException可能来自while循环中的第二个fileScanner.next()

当到达文件中的最后一个元素时,将在while循环条件下从fileScanner.next()读取该元素,从而导致在循环中进行第二次fileScanner调用时,没有剩余的元素

一种解决方案是每次迭代调用fileScanner.next()一次:

  Scanner fileScanner = new Scanner(new File("File.txt")).useDelimiter("[.,:;()?!\" \t]+~\\s");
  int totalWordCount = 0;
  Set<String> words = new HashSet<String>();
  String nextWord;
  while ((fileScanner.hasNext()) && (!words.contains(nextWord = fileScanner.next())))
  {
     words.add(nextWord);
     totalWordCount++;
  }
  System.out.println("There are " + totalWordCount + " unique word(s)");
  System.out.println("These words are:");
  System.out.println(words.toString());
  fileScanner.close();
}
Scanner fileScanner=new Scanner(新文件(“File.txt”)。使用分隔符(“[,:;()?!\”\t]+~\\s”);
int totalWordCount=0;
Set words=新HashSet();
字符串nextWord;
而((fileScanner.hasNext())&&(!words.contains(nextWord=fileScanner.next()))
{
单词。添加(nextWord);
totalWordCount++;
}
System.out.println(“有”+totalWordCount+“唯一单词”);
System.out.println(“这些词是:”);
System.out.println(words.toString());
fileScanner.close();
}

这给了我一个错误:Indexer.java:10:error:不兼容的类型:ArrayList无法转换为Set Set words=new ArrayList();我的错误,它应该是新的HashSetI当我这样做时,仍然会得到一个NosTouchElementException。您的响应应该是为了解决我的问题还是仅仅为了改进我的代码?这给了我一个错误:Indexer.java:10:error:不兼容的类型:ArrayList无法转换为Set-words=new ArrayList();我的错误,它应该是新的hashset。当我这样做的时候,我仍然得到一个NosTouchElementException。你的回答应该是为了解决我的问题还是仅仅为了改进我的代码?我试过了,但它不起作用。我有一个很长的java代码作为输入,我的输出是“有1个唯一的单词”。这些单词是:“然后是整个输入。哦,我刚刚解决了NoSuchElementException。使用集合而不是列表无疑是@Pulse9的关键所在。列表将存储每个单词实例;而集合将只存储唯一的实例。我尝试了它,但它不起作用。我有一个很长的java代码作为我的输入,我的输出是“有1个唯一的单词,这些单词是:”然后是整个输入。哦,我刚刚解决了NoSuchElementException。使用集合而不是列表无疑是@Pulse9的关键所在。列表将存储每个单词实例;而集合将只存储唯一的实例。谢谢!迭代器起了作用。我对它做了一些调整,它按照我想要的方式工作。谢谢非常感谢。迭代器起了作用。我对它做了一些调整,它按照我想要的方式工作。谢谢