Compiler: Line 39 { expected

From: Andrew (awallwork_at_gmail.com)
Date: 02/24/05


Date: 24 Feb 2005 08:48:18 -0800

Hi

When I compile the following code, I get 4 errors, the first of which
is really bugging me. I've checked my braces, and there doesn't seem
to be anything wrong with them, all methods appear to be braced. Other
than that, I've had some issues with "cannot make a static reference to
nonstatic variable" errors which do not make sense.

This is a very basic battleship program, designed to take xy
co-ordinates from one user, then another user randomly guesses xy
co-ordinates, eventually sinking all of the user placed ships. This is
for a computer science class, I'v run it past my instructor who has
gone over it, and isn't having much luck finding anything wrong there.

If anyone could shed some light into the issue, it would be greatly
appreciated.

import java.*;
import EasyReader;

public class BattleShip

{

int hits; // defines the number of hits
int shots; // defines shots fired
int percentage; // defines a percentage of shots hit vs shots fired
int x;
static int grid [][]; // defines the grid
EasyReader console = new EasyReader ();

public static void main() // loads all the methods in sequence and
loads EasyReader
        {
        fillGrid();
        addShips();
        playGame();
        }

public static void fillGrid() // fills the grid with false tags
{

int x;
int y;

                for (x=1; x<=40; x++)
                              {
                               for (y=1; y<=40; y++)
                                   { grid[y][x] = 'F'; }
                             }

}

public static void addShips()
{
static int x = 0;
int y = 0;
int c = 0;
for (c = 1; c <= 5; c++)
                {

                System.out.println("Please Enter X Co-Ordinate");
                x = console.readInt;
                System.out.println("Please Enter y Co-Ordinate");
                y = console.readInt;

                // checks the grid to ensure ship isn't there if not, it places a
ship in place
                if (grid [y][x] == 'T')
                        {
                                System.out.println("You already have a ship there");
                        }

                        else
                        {
                                grid [y][x]= true;
                        }

                  }
}

public static void playGame() // plays the game and calculates winloss
{

int x = 0;
int y = 0;

// creates loop that only allows 10 shots

do
{
System.out.println("Please Enter X Co-Ordinate to fire at");
x = console.readInt;
System.out.println("Please Enter y Co-Ordinate to fire at");
y = console.readInt;

if (grid [y][x] = 'T')
{
        System.out.println("HIT");
        hits = hits+1;
        shots = shots+1;
        System.out.println("You have hit" +((hits/shots)*100) +"% in your
game");
        grid [y][x]= 'F';
}

else
{
        System.out.println("NO HIT");
        shots = shots+1;
        System.out.println("You have hit" +((hits/shots)*100) +"% in your
game");
}

} while ( shots <= 10 || hits ==5);

}

}



Relevant Pages

  • Re: Multithreading / Scalability
    ... int thread_number; ... catch (InterruptedException e) ... public static void main{ ... every increase in threads reduced the number of calculations that could be performed although again not as dramatically as I expected with the increase in number of threads. ...
    (comp.lang.java.programmer)
  • Re: Using java.util.map
    ... public static void main{ ... The problem is not with the Map implemetations but with the handling of the ... In Java you used an imutable one. ... Is is partially the fault of the Java language in not making int a true ...
    (comp.lang.java.advocacy)
  • Re: uninitialized variable
    ... public static void main(String args) { ... int y = 10; ... But as far as the compiler ...
    (comp.lang.java.programmer)
  • Re: last version
    ... public static void main ... JSlider slider = a.getSource; ... int fontSize = source.getValue; ... Sometimes I'm in a good mood. ...
    (comp.lang.java.help)
  • RE: New JDBC 1.2 driver runs slower than JDBC 1.1 driver-- expecte
    ... public static void main ... Driver JDBCDriver = DriverManager.getDriver; ... int iSeqIx = 1; ... SignatureCaptureDT, PersonGID, LastModPersonGID, IsCCmUSer, HasBeenCCMUser" + ...
    (microsoft.public.sqlserver.jdbcdriver)