Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: I have an error and need help.

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I have an error and need help.

    I can compile the code, but when I try to run the program it says that it cannot find the main class. Any help would be greatly appreciated.

    package tictactoe;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    /**
     *
     * 
     */
    public class Main {
     
        public void play() {
     
            TicTacToe game = new TicTacToe();
     
     
            boolean continuePlaying = true;
     
            while (continuePlaying) {
     
                game.init();
                System.out.println();
                System.out.println();
                System.out.println(game.drawBoard());
                System.out.println();
     
                String player = null;
                while (!game.winner() && game.getPlays() < 9) {
                    player = game.getPlayer() == 1 ? game.getWinX() : game.getWinO();
                    boolean validPick = false;
                    while (!validPick) {
                        String square = game.getPrompt();
                        if (square.length() == 1 && Character.isDigit(square.toCharArray()[0])) {
                            int pick = 0;
                            try {
                                pick = Integer.parseInt(square);
                            } catch (NumberFormatException e) {
                              }
                            validPick = game.placeMarker(pick);
                        }
                        if (!validPick) {
                            System.out.println("Square can not be selected. Retry");
                        }
                    }
                    game.switchPlayers();
                    System.out.println();
                    System.out.println(game.drawBoard());
                    System.out.println();
                }
                if (game.winner()) {
                    System.out.println("Game Over - " + player + " WINS!!!");
                } else {
                    System.out.println("Game Over - Draw");
                }
     
            }
        }

    	private class TicTacToe{
     
     
        private char[][] board = new char[3][3];
        private String winX;
        private String winO;
        private int player;
        private char marker1;
        private char marker2;
        private int plays;
        private BufferedReader reader =
                new BufferedReader(new InputStreamReader(System.in));
     
        protected void init() {
            int counter = 0;
            for (int indexR = 0; indexR < 3; indexR++) {
                for (int indexC = 0; indexC < 3; indexC++) {
                    board[indexR][indexC] = Character.forDigit(++counter, 10);
                }
            }
            player = 1;
            plays = 0;
        }
     
        protected void switchPlayers() {
            if (getPlayer() == 1) {
                setPlayer(2);
            } else {
                setPlayer(1);
            }
            setPlays(getPlays() + 1);
        }
     
        protected boolean placeMarker(int play) {
            for (int indexR = 0; indexR < 3; indexR++) {
                for (int indexC = 0; indexC < 3; indexC++) {
                    if (board[indexR][indexC] == Character.forDigit(play, 10)) {
                        board[indexR][indexC] = (getPlayer() == 1) ? getMarker1() : getMarker2();
                        return true;
                    }
                }
            }
            return false;
        }
     
        protected boolean winner() {
            //Checking rows
            char current = ' ';
            for (int indexR = 0; indexR < 3; indexR++) {
                int indexC = 0;
                for (indexC = 0; indexC < 3; indexC++) {
                    if (!Character.isLetter(board[indexR][indexC])) {
                        break;
                    }
                    if (indexC == 0) {
                        current = board[indexR][indexC];
                    } else if (current != board[indexR][indexC]) {
                        break;
                    }
                    if (indexC == 2) {
                        //Found winner
                        return true;
                    }
                }
            }
            //Checking columns
            for (int indexR = 0; indexR < 3; indexR++) {
                current = ' ';
                int indexC = 0;
                for (indexC = 0; indexC < 3; indexC++) {
                    if (!Character.isLetter(board[indexC][indexR])) {
                        break;
                    }
                    if (indexC == 0) {
                        current = board[indexC][indexR];
                    } else if (current != board[indexC][indexR]) {
                        break;
                    }
                    if (indexC == 2) {
                        //Found winner
                        return true;
                    }
                }
            }
            //Checking diagonals
            current = board[0][0];
            if (Character.isLetter(current) && board[1][1] == current && board[2][2] == current) {
                return true;
            }
            current = board[2][0];
            if (Character.isLetter(current) && board[1][1] == current && board[0][2] == current) {
                return true;
            }
            return false;
        }
        protected String getPrompt() {
            String prompt = "";
            try {
                prompt = reader.readLine();
            } catch (IOException ex) {
            }
            return prompt;
        }
     
        protected String drawBoard() {
            StringBuilder builder = new StringBuilder("*");
            for (int indexR = 0; indexR < 3; indexR++) {
                for (int indexC = 0; indexC < 3; indexC++) {
                    builder.append("[").append(board[indexR][indexC]).append("]");
                }
                builder.append("*");
            }
            return builder.toString();
        }
     
        public int getPlayer() {
            return player;
        }
     
        public void setPlayer(int player) {
            this.player = player;
        }
     
        public char getMarker1() {
            return marker1;
        }
     
        public void setMarker1(char marker1) {
            this.marker1 = marker1;
        }
     
        public char getMarker2() {
            return marker2;
        }
     
        public void setMarker2(char marker2) {
            this.marker2 = marker2;
        }
     
        public int getPlays() {
            return plays;
        }
     
        public void setPlays(int plays) {
            this.plays = plays;
        }
     
        public String getWinX() {
            return winX;
        }
     
        public void setWinX(String winX) {
            this.winX =winX;
        }
     
        public String getWinO() {
            return winO;
        }
     
        public void setWinO(String winO) {
            this.winO = winO;
        }
    }
     
    }


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    12
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: I have an error and need help.

    You can use code tags to make your code easier to read for the rest of us.

    The error means that there's no "public static void main(String[] args)" anywhere in the class. All applications must have this method in order to run.

  3. The Following User Says Thank You to Zymus For This Useful Post:

    JavaPF (May 6th, 2011)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: I have an error and need help.

    Zymus is correct.

    I take it you Didn't write this code yourself?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #4
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have an error and need help.

    It was converted from pseudocode for a class project.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,264
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: I have an error and need help.

    Add a main() method to the class where you want execution to start when you execute the java command and reference the class: java YourClassName
    where YourClassName is the name of the class that you just added the main() method to.
    Read the doc or look at any code to see the correct syntax for the main() method.