Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8031977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:15:37+00:00 2026-06-05T01:15:37+00:00

I’m reading a text file and storing a set of unique words from that

  • 0

I’m reading a text file and storing a set of unique words from that text file into an ArrayList (please do suggest if there is a better structure for doing this). I’m using scanner to scan the text file and specifiying the delimiter as ” ” (space) as follows;

    ArrayList <String> allWords = new ArrayList <String> ();
    ArrayList <String> Vocabulary = new ArrayList <String> ();
    int count = 0;

    Scanner fileScanner = null;
    try {
        fileScanner = new Scanner (new File (textFile));

    } catch (FileNotFoundException e) {
        System.out.println (e.getMessage());
        System.exit(1);
    }

    fileScanner.useDelimiter(" ");

    while (fileScanner.hasNext()) {

        allWords.add(fileScanner.next().toLowerCase());

        count++;

        String distinctWord = (fileScanner.next().toLowerCase());
        System.out.println (distinctWord.toString());

        if (!allWords.contains(distinctWord)) {

            Vocabulary.add(distinctWord);

        }
    }

So, after printing the contents of Vocabulary, there is a word being skipped after every word. Hence for example if I have the following text file;

“The quick brown fox jumps over the lazy dog”

The contents printed are “quick fox over lazy” and then it gives me an error;

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at *java filename*.getWords(NaiveBayesTxtClass.java:82)
    at *java filename*.main(NaiveBayesTxtClass.java:22)

Could anyone please give me some suggestions on how to fix this? I have a feeling its something to do with the fileScanner.useDelimiter and fileScanner.hasNext() statements.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:15:38+00:00Added an answer on June 5, 2026 at 1:15 am

    You’re calling Scanner#next() twice after check hasNext() once,and you’re ignoring one of the returns of next().

    You call it at (1) and add it to allWords
    and call it again at (2) and print it.

    while (fileScanner.hasNext()) {
    
        allWords.add(fileScanner.next().toLowerCase()); // **** (1)
    
        count++;
    
        String distinctWord = (fileScanner.next().toLowerCase());  // **** (2)
        System.out.println (distinctWord.toString());
    
        if (!allWords.contains(distinctWord)) {
    
            Vocabulary.add(distinctWord);
    
        }
    }
    

    Solution: Call Scanner#next() once, save the String returned to a variable, then add the variable to the HashSet, and print the variable. e.g.,

    while (fileScanner.hasNext()) {
        String word = fileScanner.next().toLowerCase();
        allWords.add(word); // **** (1)
        count++;
        // String distinctWord = (fileScanner.next().toLowerCase());  // **** (2)
        System.out.println (word);
        vocabularySet.add(word); // a HashSet
    }
    

    A general rule of safety is, you should have a one-to-one relationship for each call to Scanner#hasNextXXX() and Scanner#nextXXX()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.