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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:06:19+00:00 2026-06-08T03:06:19+00:00

I am trying to get a Scanner to read the input of a text

  • 0

I am trying to get a Scanner to read the input of a text file, have that input put into a String, have a StringTokenizer of that String, and then have a String[] with each element of that array being a token of that StringTokenizer. The purpose of this is to get a String[] of the inputted text from the text file such that each element of the array is a word in the text file. However, the code I have so far generates a NoSuchElementFound Exception.

    Scanner f = new Scanner(  "input.txt" ); // Yes, I have the file path here, I changed it though. 

    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt" );
    String temp = "";
    String cString = "";

    while( ( cString = f.nextLine() ) != null ) { // Line where exception occurs

        temp += cString;
    }
    StringTokenizer everythingTokens = new StringTokenizer( temp );
    String[] everything = new String[ everythingTokens.countTokens() ];

    for( int i = 0; i < everything.length; i++ ) {
        everything[ i ] = everythingTokens.nextToken();
    }

    out.println( everything[ 0 ] );

Here is the error message

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at gift1.main(gift1.java:21)
Java Result: 1

The input for this in the text file is

Hey,
How are you?

Why is this happening and how can I fix it?

  • 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-08T03:06:20+00:00Added an answer on June 8, 2026 at 3:06 am

    That’s not how you use a Scanner. You’d instead do:

    while (scanner.hasNextLine()) {
       String line = scanner.nextLine();
       // work with your line here
    }
    

    Please have a look at the Scanner API and you’ll see that it doesn’t return null if it runs out of lines. Instead it throws a …. NoSuchElementException. I think that you’re confusing its use with that of a BufferedReader, and they’re really two completely distinct species.

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

Sidebar

Related Questions

I'm trying to read a text file and then print out the integers in
I'm trying to get each element on each line in a text file so
I'm trying to read a long type from a text file using Scanner in
trying to get same string on tooltip as is in the Text e.g. Text=<%#
From a text file I am trying to get the the 3rd set of
I have been trying to get 2,3,4 words of a file and this is
So I'm trying to get a program that reads a file, with the length
I'm trying to get Java's scanner to keep accepting input until the user types
I'm trying to read a .java file into a JTextArea and no matter what
I am trying to read some words from an online text file. I tried

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.