Choose any program from this semester that accesses a file but that does not use
ID: 3819803 • Letter: C
Question
Choose any program from this semester that accesses a file but that does not use try-catch blocks to handle file not found exceptions. Add a try and catch block to handle the exception. You can decide what the handler does. It could simply print a message – such as in a JOptionPane error message dialog box or it could allow the user to enter the correct file name.
package monopoly;
import java.util.*;
public class Monopoly
{
public static void main(String[] args) throws Exception
{
BoardSquare[] square = new BoardSquare[40];
Player[] players = new Player[8];
Dice dice;
Player player;
int rollCount;
dice = new Dice();
rollCount = 0;
int i;
loadArray(square);
loadPlayer(players);
final String SENTINEL = "quit";
final String ROLL = "roll";
int dice_totals = 0;
Scanner console = new Scanner(System.in);
System.out.print("Type "" + SENTINEL + "" to exit: ");
System.out.print("Type "" + ROLL + "" to roll dice: ");
String response = console.nextLine();
while (!response.equals(SENTINEL)) {
if (response.equals(ROLL)) {
dice.roll();
System.out.println("The dice come up " + dice.getDie1() + " and " + dice.getDie2());
int total = dice.getTotal();
int spacemax = (dice_totals - 40);
System.out.println("The dice total to " + total + " Move forward " + total + " Spaces");
if ((dice_totals + dice.getDie1() + dice.getDie2() ) > 39) {
dice_totals = (spacemax + dice.getDie1() + dice.getDie2());
}
else {
dice_totals = (dice_totals + dice.getDie1() + dice.getDie2() );
}
System.out.println(dice_totals);
System.out.println("You are on " + square[dice_totals].toString() );
System.out.print("Type a line (or "" + SENTINEL + "" to exit): ");
System.out.print("Type a line (or "" + ROLL + "" to roll dice): ");
response = console.nextLine();
}
}
}
static void loadArray(BoardSquare[] square) throws Exception
{
int i;
String inName;
String inType;
int inPrice;
int inRent;
String inColor;
java.io.File squareFile = new java.io.File("squares.txt");
Scanner infile = new Scanner(squareFile);
for( i=0; i<40; i++)
{
inName = infile.nextLine();
inType = infile.nextLine();
inPrice = Integer.parseInt( infile.nextLine() );
inRent = Integer.parseInt( infile.nextLine() );
inColor = infile.nextLine();
square[i] = new BoardSquare(inName, inType, inPrice, inRent, inColor);
}
infile.close();
}
static void loadPlayer(Player[] players) throws Exception
{
int i;
int np = 0;
boolean playable = false;
while (!playable) {
Scanner kbnp = new Scanner(System.in);
System.out.println("Enter number of Players minimum 2 , maximum 8 : ");
np = kbnp.nextInt();
if (np >= 1) {
playable = true;
}
}
for( i=0; i< np; i++)
{
players[i] = new Player();
}
}
}
Explanation / Answer
package monopoly;
import java.io.FileNotFoundException;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Monopoly
{
public static void main(String[] args) throws Exception
{
BoardSquare[] square = new BoardSquare[40];
Player[] players = new Player[8];
Dice dice;
Player player;
int rollCount;
dice = new Dice();
rollCount = 0;
int i;
loadArray(square);
loadPlayer(players);
final String SENTINEL = "quit";
final String ROLL = "roll";
int dice_totals = 0;
Scanner console = new Scanner(System.in);
System.out.print("Type "" + SENTINEL + "" to exit: ");
System.out.print("Type "" + ROLL + "" to roll dice: ");
String response = console.nextLine();
while (!response.equals(SENTINEL)) {
if (response.equals(ROLL)) {
dice.roll();
System.out.println("The dice come up " + dice.getDie1() + " and " + dice.getDie2());
int total = dice.getTotal();
int spacemax = (dice_totals - 40);
System.out.println("The dice total to " + total + " Move forward " + total + " Spaces");
if ((dice_totals + dice.getDie1() + dice.getDie2() ) > 39) {
dice_totals = (spacemax + dice.getDie1() + dice.getDie2());
}
else {
dice_totals = (dice_totals + dice.getDie1() + dice.getDie2() );
}
System.out.println(dice_totals);
System.out.println("You are on " + square[dice_totals].toString() );
System.out.print("Type a line (or "" + SENTINEL + "" to exit): ");
System.out.print("Type a line (or "" + ROLL + "" to roll dice): ");
response = console.nextLine();
}
}
}
static void loadArray(BoardSquare[] square) throws Exception
{
int i;
String inName;
String inType;
int inPrice;
int inRent;
String inColor;
java.io.File squareFile = new java.io.File("squares.txt");
try {
Scanner infile = new Scanner(squareFile);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
JFrame f=new JFrame();
JOptionPane.showMessageDialog(f,"Error - " + squareFile.getName() + " File Not Found", "Alert", JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
for( i=0; i<40; i++)
{
inName = infile.nextLine();
inType = infile.nextLine();
inPrice = Integer.parseInt( infile.nextLine() );
inRent = Integer.parseInt( infile.nextLine() );
inColor = infile.nextLine();
square[i] = new BoardSquare(inName, inType, inPrice, inRent, inColor);
}
infile.close();
}
static void loadPlayer(Player[] players) throws Exception
{
int i;
int np = 0;
boolean playable = false;
while (!playable) {
Scanner kbnp = new Scanner(System.in);
System.out.println("Enter number of Players minimum 2 , maximum 8 : ");
np = kbnp.nextInt();
if (np >= 1) {
playable = true;
}
}
for( i=0; i< np; i++)
{
players[i] = new Player();
}
}
}