Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If I was trying to throw an exception for a character that isn\'t allowed, ALLOW

ID: 3828787 • Letter: I

Question

If I was trying to throw an exception for a character that isn't allowed, ALLOWED_CHARS, where and how would I write it in this constructor?

If I was trying to throw an exception for having more than one START char, where and how would I write it in this constructor?

   private char[][] board;
   private Point startingPoint;
   private Point endingPoint;

   private final int ROWS; //initialized in constructor
   private final int COLS; //initialized in constructor
   private final char OPEN = 'O'; //capital 'o'
   private final char CLOSED = 'X';
   private final char TRACE = 'T';
   private final char START = '1';
   private final char END = '2';
   private final String ALLOWED_CHARS = "OXT12";

public CircuitBoard(String filename) throws FileNotFoundException {          
       try {
           @SuppressWarnings("resource")
           Scanner fileScan = new Scanner(new File(filename));
           ROWS = Integer.parseInt(fileScan.next()); //initialization statement using values from file
           COLS = Integer.parseInt(fileScan.next()); //initialization statement using values from file      
           board = new char[ROWS][COLS]; //parse the given file to populate the char[][]      
           int row = -1;
           int col = 0;          
           while(fileScan.hasNextLine()) {
               if(row > ROWS) {
                   throw new InvalidFileFormatException("CircuitBoard - rows");
               }
               Scanner scan2 = new Scanner(fileScan.nextLine());
               while(scan2.hasNext()) {
                   if(col > COLS) {
                       scan2.close();
                       throw new InvalidFileFormatException("CircuitBoard - columns");
                   }
                   String str = scan2.next();
                   if(str.length() > 1) {
                       scan2.close();
                       throw new InvalidFileFormatException("CircuitBoard - string");
                   }
                   else {
                       board[row][col] = str.charAt(0);
                       if(str.charAt(0) == START) {
                           startingPoint = new Point(col, row);
                       }
                       else if(str.charAt(0) == END) {
                           endingPoint = new Point(row, col);
                       }
                       col++;
                   }
               }
               col = 0;
               row++;
               scan2.close();
           }          
           fileScan.close();
       }
       catch(FileNotFoundException e) {
           System.out.println("Exception thrown: " + e);
           System.out.println("INVALID ");
           throw new FileNotFoundException("CircuitBoard - before constructor");
       }
       catch(InvalidFileFormatException e) {
           System.out.println("Exception thrown: " + e);
           System.out.println("INVALID ");
           throw new InvalidFileFormatException("CircuitBoard - before constructor");
       }
   }

Explanation / Answer

Hi, You can thow like this:

###### Exceptions Classes #######

public class InvalidFileFormatException extends Exception {

  

   public InvalidFileFormatException(String s) {

      

   }

}

class NotAllowedCharException extends Exception {

  

   public NotAllowedCharException(String s) {

       super(s);

   }

}

class StartAvailableException extends Exception {

  

   public StartAvailableException(String s) {

       super(s);

   }

}

#############

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

class Point{

   Point(int x, int j){

   }

}

public class CircuitBoard {

   private char[][] board;

   private Point startingPoint;

   private Point endingPoint;

   private final int ROWS; //initialized in constructor

   private final int COLS; //initialized in constructor

   private final char OPEN = 'O'; //capital 'o'

   private final char CLOSED = 'X';

   private final char TRACE = 'T';

   private final char START = '1';

   private final char END = '2';

   private final String ALLOWED_CHARS = "OXT12";

  

   public boolean isAllowedChar(char c){

      

       return ALLOWED_CHARS.contains(""+c);

   }

  

   public boolean isStarAvailable(){

      

       for(int i=0; i<ROWS; i++){

           for(int j=0; j<COLS; j++){

               if(board[i][j] == START){

                   return true;

               }

           }

       }

      

       return true;

   }

   public CircuitBoard(String filename) throws FileNotFoundException,

               InvalidFileFormatException, NotAllowedCharException, StartAvailableException {

       try {

           @SuppressWarnings("resource")

           Scanner fileScan = new Scanner(new File(filename));

           ROWS = Integer.parseInt(fileScan.next()); //initialization statement using values from file

           COLS = Integer.parseInt(fileScan.next()); //initialization statement using values from file

           board = new char[ROWS][COLS]; //parse the given file to populate the char[][]

           int row = -1;

           int col = 0;

           while(fileScan.hasNextLine()) {

               if(row > ROWS) {

                   throw new InvalidFileFormatException("CircuitBoard - rows");

               }

               Scanner scan2 = new Scanner(fileScan.nextLine());

               while(scan2.hasNext()) {

                   if(col > COLS) {

                       scan2.close();

                       throw new InvalidFileFormatException("CircuitBoard - columns");

                   }

                   String str = scan2.next();

                   if(str.length() > 1) {

                       scan2.close();

                       throw new InvalidFileFormatException("CircuitBoard - string");

                   }

                   else {

                      

                       if(! isAllowedChar(str.charAt(0))){

                           scan2.close();

                           throw new NotAllowedCharException("character is not allowed");

                       }

                      

                       board[row][col] = str.charAt(0);

                       if(str.charAt(0) == START) {

                          

                           if(isStarAvailable()){

                               scan2.close();

                               throw new StartAvailableException("start character alreay avaialble");

                           }

                           startingPoint = new Point(col, row);

                       }

                       else if(str.charAt(0) == END) {

                           endingPoint = new Point(row, col);

                       }

                       col++;

                   }

               }

               col = 0;

               row++;

               scan2.close();

           }

           fileScan.close();

       }

       catch(FileNotFoundException e) {

           System.out.println("Exception thrown: " + e);

           System.out.println("INVALID ");

           throw new FileNotFoundException("CircuitBoard - before constructor");

       }

       catch(InvalidFileFormatException e) {

           System.out.println("Exception thrown: " + e);

           System.out.println("INVALID ");

           throw new InvalidFileFormatException("CircuitBoard - before constructor");

       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote