Exception handling in Java - Keep executing until user enters four valid employee IDs

Viewed 366

What's wrong with this code:

DebugTwelve4.java:25: error: cannot find symbol throw(new FixDebugEmployeeIDException("Number too high " + emps[x])); ^ symbol: class FixDebugEmployeeIDException location: class DebugTwelve4 DebugTwelve4.java:36: error: cannot find symbol catch(FixDebugEmployeeIDException error) ^ symbol: class FixDebugEmployeeIDException location: class DebugTwelve4 2 errors Error: Could not find or load main class DebugTwelve4

DebugEmployeeIDException.java:

public class DebugEmployeeIDException extends Exception
{
   public DebugEmployeeIDException()
   {
      super("Debug employee exception");
   }
}

-----------------------------------------------------------
DebugTwelve4.java:

// An employee ID can't be more than 999
// Keep executing until user enters four valid employee IDs
// This program throws a FixDebugEmployeeIDException
import java.util.*;
public class DebugTwelve4
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      String inStr, outString = "";
      final int MAX = 999;
      int[] emps = new int[4];
      int x;
      try
      {
      for(x = 0; x < emps.length; ++x)
      {
        System.out.println("Enter employee ID number");
         inStr = input.next();
         throw(new NumberFormatException("Number format exception"));
         {
            emps[x] = Integer.parseInt(inStr);
            if(emps[x] > MAX)
            {
               throw(new FixDebugEmployeeIDException("Number too high " + emps[x]));
            }
         }
      }
         
      }
      catch(NumberFormatException error)
         {  
            --x;
            System.out.println(inStr + "\nNonnumeric ID");
         }
         catch(FixDebugEmployeeIDException error)
         {  
        --x;
        System.out.println("FixDebugEmployeeIDException");
         }

      for(x = 0; x < emps.length; ++x);
      {
         outString = outString + emps[x] + " ";
      }
      System.out.println("Four valid IDS are: " + outString);    
   }
}

2 Answers

Since you are writing all your codes in one method (main method), there is actually no need to throw any exceptions.

Typically, you throw an exception when you are chaining method calls and you do not want to handle the error there and then -> you throw it and propagate it back to the caller for caller to handle.

In the context of your question, you can just a simple if/else check within the loop to ensure that the input is never more than 999.

int[] employeeIds = new int[4]
final int _MAX_ = 999;

for (int i = 0 ; i < employeeIds.length; i++) {
  int input = sc.nextInt();
  if (input > _MAX_) {
    System.out.println("Invalid ID, please try again!");
    i--; // when invalid input, this ensures that the index stays the same
    continue;
  } else {
    employeeIds[i] = input;
  }
}

// loop through employeeIds and print your output

Below works for me.

public class DebugEmployeeIDException extends Exception
{
    public DebugEmployeeIDException()
    {
        super("Debug employee exception");
    }
    public DebugEmployeeIDException(String message) {
        super(message);
    }
}


import java.util.*;
public class DebugTwelve4
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String inStr="", outString = "";
        final int MAX = 999;
        int[] emps = new int[4];
        int x=0;
        try
        {
            for(x = 0; x < emps.length; ++x)
            {
                System.out.println("Enter employee ID number");
                inStr = input.next();
                emps[x] = Integer.parseInt(inStr);
                if(emps[x] > MAX)
                {
                    throw new DebugEmployeeIDException("Number too high " + emps[x]);
                }
            }

        }
        catch(NumberFormatException error)
        {  
            --x;
            System.out.println(inStr + "\nNonnumeric ID");
        }
        catch(DebugEmployeeIDException error)
        {  
            --x;
            System.out.println("FixDebugEmployeeIDException");
        }


        for(x = 0; x < emps.length;x++)
        {
            outString = outString + emps[x] + " ";
        }
        System.out.println("Four valid IDS are: " + outString);    
    }
}
Related