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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:05:05+00:00 2026-05-30T00:05:05+00:00

I’m getting the below exception in my java project. Exception in thread main java.util.NoSuchElementException

  • 0

I’m getting the below exception in my java project.

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at com.ooad.ooadfirstassignment.Employee.readData(Employee.java:25)
    at com.ooad.ooadfirstassignment.Salaried.readData(Salaried.java:16)
    at com.ooad.ooadfirstassignment.Factory.<init>(Factory.java:21)
    at com.ooad.ooadfirstassignment.MainClass.main(MainClass.java:25)

The Code is as follows :

=====================================================

public class MainClass {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException
    {
        // TODO Auto-generated method stub
        FileInputStream empTextStreamIn = null;
        try
        {
            empTextStreamIn = new FileInputStream("Employee.txt");
        }
        catch(FileNotFoundException fex)
        {
            System.out.println("Employee File not found");
            fex.printStackTrace();
        }
        Factory f = new Factory(empTextStreamIn);
        empTextStreamIn.close();

    }

}

====================================================================

public class Factory 
{
    public Factory(FileInputStream empTextStreamIn) 
    {
        // TODO Auto-generated constructor stub
        int empType;
        String EmpID = null,DeptID = null;
        double salary=0;
        Scanner sc = new Scanner(empTextStreamIn);
        while(sc.hasNextLine())
        {
            empType = sc.nextInt();
            switch (empType) 
            {
                case 1:Salaried salr = new Salaried(empTextStreamIn);
                        salr.readData();

                                      System.out.println("EmpType="+empType+"   EmpID="+EmpID+"   DeptID="+DeptID+"   Salary="+salary);

The code goes on so this is the main part though.

Next the Employee Class

public class Employee 
{
    String EmpID, DeptID;   //Unique detail for Employee class

    protected Scanner sc;

    Employee()
    {

    }
    public Employee(FileInputStream empTextStreamIn) 
    {
        // TODO Auto-generated constructor stub
        sc = new Scanner(empTextStreamIn);

    }
    void readData()
    {
        String EmpID = sc.next();
        String DeptID = sc.next();
    }

}

Salaried Class

public class Salaried extends Employee
{
    double salary;  //Unique detail for Salaried class

    public Salaried(FileInputStream empTextStreamIn) 
    {
        super(empTextStreamIn);

    }
    void readData()
    {
        super.readData();
        salary = sc.nextDouble();


    }

}

Kindly help me where I’m going wrong.

  • 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-05-30T00:05:06+00:00Added an answer on May 30, 2026 at 12:05 am

    The problem here is that the Scanner class uses an internal buffer. You create a Scanner object in the Factory class. This Scanner reads from the underlying FileInputStream into its own buffer. I suspect your input file is so small that the whole file fits into this buffer. That means that the Scanner in the Factory will read through the whole FileInputStream. After this, you create a new Scanner object in the Employee class, using the same FileInputStream. However, the first Scanner has already consumed all the content in that FileInputStream. Hence, because there is no data, that Scanner throws an exception.

    So what you need is probably just to ensure that you use the same Scanner object when reading from the file, instead of instantiating several different scanners on the same FileInputStream.

    <EDIT>

    To verify that this is the case, you can try inserting the following line inside the while loop in the Factory class:

    System.out.println("Data could be read from the InputFileStream: " 
                        + (empTextStreamIn.read() != -1));
    

    (Note that the read method can throw an IOException, so you will have to surround it with a try-catch block)

    </EDIT>

    • 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
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.