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 8984243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:03:06+00:00 2026-06-15T21:03:06+00:00

I am creating a simple program that counts the number of words, lines and

  • 0

I am creating a simple program that counts the number of words, lines and total characters (not including whitespace) in a paper. It is a very simple program. My file compiles but when I run it I get this error:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at WordCount.wordCounter(WordCount.java:30)
    at WordCount.main(WordCount.java:16)

Does anyone know why this is happening?

 import java.util.*;
 import java.io.*;
 public class WordCount {
//throws the exception
public static void main(String[] args) throws FileNotFoundException {
    //calls on each counter method and prints each one
    System.out.println("Number of Words: " + wordCounter());
    System.out.println("Number of Lines: " + lineCounter());
    System.out.println("Number of Characters: " + charCounter());

}

//static method that counts words in the text file  
public static int wordCounter() throws FileNotFoundException { 
//inputs the text file
Scanner input = new Scanner(new File("words.txt")); 
    int countWords = 0;
    //while there are more lines
    while (input.hasNextLine()) {
        //goes to each next word
        String word = input.next();
        //counts each word
        countWords++;
    }
    return countWords;
}

//static method that counts lines in the text file  
public static int lineCounter() throws FileNotFoundException {
//inputs the text file
Scanner input2 = new Scanner(new File("words.txt"));
    int countLines = 0;
    //while there are more lines
    while (input2.hasNextLine()) {
        //casts each line as a string
        String line = input2.nextLine();
        //counts each line
        countLines++;
    }
    return countLines;
   }    

//static method that counts characters in the text file 
public static int charCounter() throws FileNotFoundException {
//inputs the text file
Scanner input3 = new Scanner(new File("words.txt"));
    int countChar = 0;
    int character = 0;
    //while there are more lines
    while(input3.hasNextLine()) {
       //casts each line as a string
        String line = input3.nextLine();
        //goes through each character of the line
        for(int i=0; i < line.length(); i++){
            character = line.charAt(i);
             //if character is not a space (gets rid of whitespace)
            if (character != 32){
                //counts each character
                countChar++;
            }
        }           
    }
   return countChar;
}
} 
  • 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-15T21:03:07+00:00Added an answer on June 15, 2026 at 9:03 pm

    I can’t really say the exact reason for the problem without looking at the file (Maybe even not then).

       while (input.hasNextLine()) {
            //goes to each next word
            String word = input.next();
            //counts each word
            countWords++;
        }
    

    Is your problem. If you are using the input.hasNextLine() in the while conditional statement use input.nextLine(). Since you are using input.next() you should use input.hasNext() in the while loops conditional statement.

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

Sidebar

Related Questions

So I'm working on creating a very simple C program that just preforms shell
I'm creating my own very simple program that allows the user to input a
We have a fairly simple program that's used for creating backups. I'm attempting to
I am teaching myself Common Lisp and I'm creating a simple program that interfaces
I am here including a simple program that written in C++ where I am
I was creating a simple program that simulates a coin toss for my class.
I am creating a simple notification program and I am using WCF For that.
I am creating a simple python program that needs to search a somewhat large
A lot of time when creating simple events in my program that other classes
I'm just creating a simple program that gets user input then prints it back

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.