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.
The problem here is that the
Scannerclass uses an internal buffer. You create aScannerobject in theFactoryclass. ThisScannerreads from the underlyingFileInputStreaminto its own buffer. I suspect your input file is so small that the whole file fits into this buffer. That means that theScannerin theFactorywill read through the wholeFileInputStream. After this, you create a new Scanner object in theEmployeeclass, using the sameFileInputStream. However, the firstScannerhas already consumed all the content in thatFileInputStream. Hence, because there is no data, thatScannerthrows an exception.So what you need is probably just to ensure that you use the same
Scannerobject when reading from the file, instead of instantiating several different scanners on the sameFileInputStream.<EDIT>
To verify that this is the case, you can try inserting the following line inside the while loop in the
Factoryclass:(Note that the read method can throw an IOException, so you will have to surround it with a try-catch block)
</EDIT>