#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2

    NoSuchElement ... and I don't know why


    So this is my list of methods
    Code:
    import java.text.*;
    import java.util.*;
    
    /**
    This file provided to the student for modification
    
    The Payroll class stores data about an employee's pay
    */
    
    public class Payroll
    {
       private String name;          // Employee name
       private int idNumber;         // ID number
       private double payRate;       // Hourly pay rate
       private double hoursWorked;   // Number of hours worked
    
       /**
          The constructor initializes an object with the
          employee's name and ID number.
          @param n The employee's name.
          @param i The employee's ID number.
       */
    
       public Payroll(String n, int i)
       {
          name = n;
          idNumber = i;
       }
    
       /**
          The setName sets the employee's name.
          @param n The employee's name.
       */
    
       public void setName(String n)
       {
    	   name = n;
       }
    
       /**
          The setIdNumber sets the employee's ID number.
          @param i The employee's ID number.
       */
       
       public void setIdNumber(int i)
       {
          idNumber = i;
       }
    
       /**
          The setPayRate sets the employee's pay rate.
          @param p The employee's pay rate.
       */
       
       public void setPayRate(double p)
       {
          payRate = p;
       }
    
       /**
          The setHoursWorked sets the number of hours worked.
          @param h The number of hours worked.
       */
    
       public void setHoursWorked(double h)
       {
          hoursWorked = h;
       }
    
       /**
          The getName returns the employee's name.
          @return The employee's name.
       */
    
       public String getName()
       {
    	   
          return name;
       }
    
       /**
          The getIdNumber returns the employee's ID number.
          @return The employee's ID number.
       */
       
       public int getIdNumber()
       {
          return idNumber;
       }
    
       /**
          The getPayRate returns the employee's pay rate.
          @return The employee's pay rate.
       */
    
       public double getPayRate()
       {
          return payRate;
       }
    
       /**
          The getHoursWorked returns the hours worked by the
          employee.
          @return The hours worked.
       */
    
    
       public double getHoursWorked()
       {
          return hoursWorked;
       }
    
       /**
          The getGrossPay returns the employee's gross pay.
          @return The employee's gross pay.
       */
    
       public double getGrossPay()
       {
          return hoursWorked * payRate;
       }
       public String toString()
       {
    	   NumberFormat df = new DecimalFormat("$#0.00");
    	   return (name + ": " + idNumber + " " + df.format(getGrossPay()));
       }
       public static String IOName() 
       {
    	   Scanner s = new Scanner(System.in);
    	   System.out.println("Enter employee name: ");
    	   String response = s.nextLine();
    	   while (response.equals(""))
    	   {
    		   System.out.println("Please Enter Name to continue ");
    		   response = s.nextLine();
    	   }
    	   s.close();
    	   return response;
       }
       public static int IONumber() 
       {
    	   Scanner s = new Scanner(System.in);
    	   System.out.println("Enter ID number: ");
    	   int response = Integer.parseInt(s.nextLine());
    	   return response;
       }
    }
    And this is my controller:
    Code:
    import java.util.*;
    
    /**
    * (description of the project)
    *
    * @author Darris Hawks
    * @version 1.0
    */
    public class PayrollApp {
    	
    	
    	public static void main (String args []) 
    	{
    	   	ArrayList <Payroll> list = new ArrayList <>();
    		Scanner s = new Scanner(System.in);
    	   	int x = 0;
    		Payroll roll = new Payroll(null, 0);
    		while (x == 0) 
    		{
    			
    			roll.setName(Payroll.IOName());
    		
    			int z = 0;
    			while (z ==0)  
    			{
    				try 
    				{
    					roll.setIdNumber(Payroll.IONumber());
    			    	list.add(roll);
    			    	z++;
    				} 
    				catch (NumberFormatException nfe)    
    				{
    					System.out.println("ID number must consist of only numbers. Try again");
    				}
    				catch(NoSuchElementException e)
    				{
    					//leave it.....
    				}
    			}// end while
    	
    
    		System.out.println("Add another employee? (y/n) ");
    		String response2 = s.nextLine();
    			if (!response2.equalsIgnoreCase("y"))
    			{
    				x ++;
    			}
    		}
    		System.out.println(list.toString());
    		
    	}//end main
    }
    But when I try to run it, it loops through
    Code:
    Enter name:
    Enter ID number: 
    Enter ID number: 
    Enter ID number: 
    Enter ID number: 
    Enter ID number: 
    Enter ID number: 
    Enter ID number: 
    Enter ID number: 
    Enter ID number: 
    Enter ID number: 
    Enter ID number:
    I don't know what's causing this exception. I've searched the forums, but I don't see that any of the issues the others are having are related to mine.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Note that you have multiple Scanners created that are connected to System.in. I would suggest using just the Scanner you create in PayrollApp and send that as a parameter to IOName and IONumber.

IMN logo majestic logo threadwatch logo seochat tools logo