Hey all, first post, hope I'm not doing anything wrong

I'm taking a Java module next term at university, and in preparation, they want us to code two things. They reckon there's a help file up on their intranet but the intranet is very unstable, and doesn't seem to handle more than 0 users at a time

Anyway, I'll copy and paste from the handout they've given us so I'm not confusing anyone:

ARRAYS

Problem 1: TicTacToe

Read a three by three lines of noughts and crosses. Print "true" if the crosses side has won the game; print false otherwise.

Problem 2: Magic Square

Read in four lines of four numbers separated by the tab character. Print "true" if these form a “magic square”; print "false" otherwise. A magic square is one where all the number in each row, column and the diagonals add up to the same number:

Any help is much appreciated - I have done practically no Java up to this point, so i would be grateful if you could keep it as simple as possible

Recommended Answers

All 20 Replies

The post at the top of this forum should get you started.

Also I didn't see a question in your post. What do you expect us to do for you?

Sorry, not used to the way things work here yet!

I have taught myself how to set up an array and enter data into it, but what I can't do is to then read data from the array.

For example, in the tic-tac-toe problem, I have set up the grid, and can input where the o's and x's are, but I don't then know how to tell the program to calculate who has won

I have tried using if (x[0][0] == x[0][1]...etc.), but this throws errors back at me

I was hoping that someone would be able to give me a starter on how to do this

I have tried using if (x[0][0] == x[0][1]...etc.), but this throws errors back at me

And what are those errors Java is normally pretty clear while specifying any errors in code.


And your thread title almost sounds like an ad in a matrimonial columns :P .

I have tried using if (x[0][0] == x[0][1]...etc.), but this throws errors back at me

what kind of errors? and what type of data did you store in that array? String, int, char, ...?

import javax.swing.JOptionPane;

class areaDemo {
     public static void main(String[] args) {
          int[][] area;              // declares  array of integers

          final int NOUGHT = 1; //denotes a nought
          final int CROSS = 2; //denotes a cross
          
          area = new int[3][3];      // allocates memory for 10 integers
            
          area[0][0] = NOUGHT; // initialize first element
          area[0][1] = NOUGHT; // initialize second element
          area[0][2] = NOUGHT; // etc.
          area[1][0] = CROSS;
          area[1][1] = NOUGHT;
          area[1][2] = CROSS;
          area[2][0] = NOUGHT;
          area[2][1] = CROSS;
          area[2][2] = NOUGHT;
          
          if (area[0][0] == area [0][1] && area [0][1] == area [0][2]){
            System.out.println("true");}
          
         
     }
}

That is the code that I have so far, the error given is 'illegal start of expression' at the if statement

EDIT: I realise that some of the \\ points are wrong, I adapted the code from a program that I already coded

I run the program and it works ok.

I just ran your code and it works just fine
you forgot to compile it or something?

That's irritating to say the least, I just re-compiled and re-ran it and it throws out the same error (I'm using DrJava btw) :confused:

Can you suggest a different way of coding the problem then? Perhaps if I read the nought and cross positions in from a text file?

Can you at least tell us what the compiler is spitting out at you ???

File: C:\Users\Jamie\Documents\Napier Stuff\Dr Java\nx.java [line: 22]
Error: C:\Users\Jamie\Documents\Napier Stuff\Dr Java\nx.java:22: illegal start of expression

nx.java being the savename until I got the program working

your class is areaDemo while your file name is nx ??? Change the file name to areaDemo and thing will be fine.

I thought it was alright as long as the class wasn't defined as public?

Anyway, no change, it still spits out the same thing

Anyway, no change

no change here either, it still runs perfectly ... don't really see where I can help at that :/

Can you again post the code, the error, and the commands you are using to compile and run

I would suggest compiling the program from your console and check whether you are still getting the same error.

gah!

alright, thanks anyway

import javax.swing.JOptionPane;

class nx {
     public static void main(String[] args) {
          int[][] area;              // declares  array of integers

          final int NOUGHT = 1; //denotes a nought
          final int CROSS = 2; //denotes a cross
          
          area = new int[3][3];      // allocates memory for 10 integers
            
          area[0][0] = NOUGHT; // initialize first element
          area[0][1] = NOUGHT; // initialize second element
          area[0][2] = NOUGHT; // etc.
          area[1][0] = CROSS;
          area[1][1] = NOUGHT;
          area[1][2] = CROSS;
          area[2][0] = NOUGHT;
          area[2][1] = CROSS;
          area[2][2] = NOUGHT;
          
          if (area[0][0] == area [0][] && area [0][1] == area [0][2]){
            System.out.println("true");}
          
         
     }
}

The error ('illegal start of expression') occurs on line 22, ie. the line beginning if(area....)

I am using Dr. Java to code, and it comes with a built-in compiler. I have since tried compiling using the console as suggested by stephen, but it also gives me the same error

== area [0][]

here's your error.
that wasn't present the last time you posted the code

strange, I haven't touched the code since I started this thread - oh well, looks like problem solved anyway, thanks everyone

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.