Image

hi. i can't get this program to catch the exceptions. there are other classes to it. BattleShipMain, BattleShip and ProgramTerminator... but I know the error lies here. and i'm pretty sure that it's in the actionPerformed and getGuess classes. if anyone can help out, that'd be great. this is actually due tomorrow...and i'm an idiot and thought i'd be able to get help earlier. thanks in advance.

import javabook.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

class BattleShipUI extends Frame implements ActionListener
{
int grid[][] = {};
int guess[] = new int[2];
int guessx, guessy;
boolean guessReady;
Label xLabel, yLabel, outputLabel;
TextField xInput, yInput;

public BattleShipUI()
{
setSize(350,350);
setTitle("Battleship!");
setResizable(false);
setLayout(null);
reset();
addWindowListener(new ProgramTerminator());
setVisible(true);
}

public void displayGrid(int[][] grid)
{
this.grid = grid;
repaint();
}

public void reset()
{
removeAll();
initComponents();
guessReady = false;
}

public void initComponents()
{
xLabel = new Label("x value");
xInput = new TextField( 10 );
yLabel = new Label("y value");
yInput = new TextField( 10 );
Button fire = new Button("Fire!");
fire.addActionListener(this);
fire.setBounds(230+(int)17, 60+137, 90, 20);
xLabel.setBounds(230+(int)17, 60+17, 90, 30);
xInput.setBounds(230+(int)17, 60+47, 90, 20);
yLabel.setBounds(230+(int)17, 60+77, 90, 30);
yInput.setBounds(230+(int)17, 60+107, 90, 20);
add(xLabel);
add(xInput);
add(yLabel);
add(yInput);
add(fire);
}

public void paint(Graphics g)
{
super.paint(g);
for(int i=0;i
[Error: Irreparable invalid markup ('<grid.length;i++)>') in entry. Owner must fix manually. Raw contents below.]

hi. i can't get this program to catch the exceptions. there are other classes to it. BattleShipMain, BattleShip and ProgramTerminator... but I know the error lies here. and i'm pretty sure that it's in the actionPerformed and getGuess classes. if anyone can help out, that'd be great. this is actually due tomorrow...and i'm an idiot and thought i'd be able to get help earlier. thanks in advance.
<lj-cut text="BattleShipUI.java">
import javabook.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

class BattleShipUI extends Frame implements ActionListener
{
int grid[][] = {};
int guess[] = new int[2];
int guessx, guessy;
boolean guessReady;
Label xLabel, yLabel, outputLabel;
TextField xInput, yInput;

public BattleShipUI()
{
setSize(350,350);
setTitle("Battleship!");
setResizable(false);
setLayout(null);
reset();
addWindowListener(new ProgramTerminator());
setVisible(true);
}

public void displayGrid(int[][] grid)
{
this.grid = grid;
repaint();
}

public void reset()
{
removeAll();
initComponents();
guessReady = false;
}

public void initComponents()
{
xLabel = new Label("x value");
xInput = new TextField( 10 );
yLabel = new Label("y value");
yInput = new TextField( 10 );
Button fire = new Button("Fire!");
fire.addActionListener(this);
fire.setBounds(230+(int)17, 60+137, 90, 20);
xLabel.setBounds(230+(int)17, 60+17, 90, 30);
xInput.setBounds(230+(int)17, 60+47, 90, 20);
yLabel.setBounds(230+(int)17, 60+77, 90, 30);
yInput.setBounds(230+(int)17, 60+107, 90, 20);
add(xLabel);
add(xInput);
add(yLabel);
add(yInput);
add(fire);
}

public void paint(Graphics g)
{
super.paint(g);
for(int i=0;i<grid.length;i++)
{
for(int j=0;j<grid[i].length;j++)
{
if(grid[i][j] == 2 || grid[i][j] == 0)
g.setColor(Color.black);
else if(grid[i][j] == 1)
g.setColor(Color.gray);
else if(grid[i][j] == -1)
g.setColor(Color.blue);
g.fillRect(50+j*10, 50+i*10, 10, 10);
}
}
}

public int[] getGuess()
{
try
{
while(!guessReady)
Thread.sleep(50);
} catch(Exception z)
{
}
guessReady = false;
return guess;
}

public void actionPerformed(java.awt.event.ActionEvent actionEvent)
{
String x = actionEvent.getActionCommand();
int guessx = Integer.parseInt(xInput.getText());
int guessy = Integer.parseInt(yInput.getText());
guess[0] = (int)(guessy - 1);
guess[1] = (int)(guessx - 1);
if(x.equals("Fire!"))
if(guess[0] > -1 && guess[0] < 11 && guess[1] > -1 && guess[1] < 11){
guessReady = true;
}
else {
guessReady = false;
outputLabel = new Label("Your guess is invalid.");
outputLabel.setBounds( 230+17, 60+167, 90, 30 );
add(outputLabel);
reset();
}
}
}
class ProgramTerminator implements WindowListener
{
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.exit(0);
}
public void windowActivated(java.awt.event.WindowEvent windowEvent) {
}
public void windowDeiconified(java.awt.event.WindowEvent windowEvent) {
}
public void windowDeactivated(java.awt.event.WindowEvent windowEvent) {
}
public void windowIconified(java.awt.event.WindowEvent windowEvent) {
}
public void windowClosed(java.awt.event.WindowEvent windowEvent) {
}
public void windowOpened(java.awt.event.WindowEvent windowEvent) {
}