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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:21:32+00:00 2026-06-14T20:21:32+00:00

I am trying to write a program that reads a number of files and

  • 0

I am trying to write a program that reads a number of files and compares them line by line. The number of files to be read are taken from the user, and the files are expected to be in the form “input1.txt”, “input2.txt” and so on.

When I try to run my code I get a “NoSuchElementException” telling me that Scanner.nextLine() line: line not avaialble. This is at line 50, which is: jthLine.add(myScanners.get(k - 1).nextLine());. I can’t figure out why there wouldn’t be a line to read as this is already done in a loop that is bounded by the minimum number of lines.

Any help is appreciated! Here is my code:

// Compares n input files, prints the lines that are not equal in all.

import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class CompareFiles {
public static void main(String[] args) throws IOException {
    Scanner consoleScanner = new Scanner(System.in);
    int numberOfFiles;
    System.out.println("Please enter how many files are there. The" +
            "file names should be like: input1.txt input2.txt, etc.");
    numberOfFiles = consoleScanner.nextInt();

    ArrayList<Scanner> myScanners = new ArrayList<Scanner>();

    for (int k = 1; k <= numberOfFiles; k++)
        myScanners.add(new Scanner(new File("input" + k + ".txt")));

    // Find the file line counts.
    ArrayList<Integer> lineCounts = new ArrayList<Integer>();
    Integer lineCount;
    String increment;

    for (int i = 1; i <= numberOfFiles; i++) {
        lineCount = 0;

        while (myScanners.get(i - 1).hasNext()) {
            increment = myScanners.get(i - 1).nextLine();
            lineCount++;
        }

        lineCounts.add(lineCount);
    }

    // Find the minimum file count.
    int minLineCount = Collections.min(lineCounts);

    // Compare files until minLineCount line by line
    // println all the unmatching lines from all files
    // jthLine holds the incremented lines from all files
    ArrayList<String> jthLine = new ArrayList<String>();
    boolean allMenAreTheSame;

    for (int j = 1; j <= minLineCount; j++) {
        allMenAreTheSame = true; // are all jth lines the same?

        for (int k = 1; k <= numberOfFiles; k++) {
            jthLine.add(myScanners.get(k - 1).nextLine());

            if (!jthLine.get(0).equals(jthLine.get(k)))
                allMenAreTheSame = false;
        }

        if (!allMenAreTheSame) {
            System.out.println();
            System.out.println("Line number " + j + " is not the same" + 
            "across all files, printing the lines:");

            for (int k = 1; k <= numberOfFiles; k++) {
                System.out.println("File: \"input" + k + ".txt\":");
                System.out.println(jthLine.get(k - 1));
            }
        }

        jthLine.clear();
    }
}

}

  • 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-14T20:21:33+00:00Added an answer on June 14, 2026 at 8:21 pm

    The reason you are getting this error is because while counting the number of lines the scanner object myScanners.get(i - 1) reaches the end of file. So when you try to read from it again, it starts from where you left i.e. the end of file.

    There are different ways, but I think the easiest would be to create new scanner objects.

     for (int i = 1; i <= numberOfFiles; i++) {
            lineCount = 0;
    
            while (myScanners.get(i - 1).hasNext()) {
                increment = myScanners.get(i - 1).nextLine();
                lineCount++;
            }
    
            lineCounts.add(lineCount);
        }
    
     myScanners.clear()
    
     for (int k = 1; k <= numberOfFiles; k++)
            myScanners.add(new Scanner(new File("input" + k + ".txt")));
    
     // Find the minimum file count.
    int minLineCount = Collections.min(lineCounts);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that reads 2 numbers from the user and
I'm trying to write a program that reads 2 numbers from the user and
I am trying to write a program that sends basic text files from one
I am trying to write a program that reads in binary files using C++.
I am trying to write a program that reads from the console a positive
I am trying to write a simple program that reads integers from a data
I'm trying to write a little C# program that reads from a text file
I am trying to write a program that will read just the first line
im trying to write a program that reads from a text file into a
I'm trying to write a program that reads broadcasted UDP datagrams in linux.I'm a

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.