I can't figure out why I'm getting this error, i've checked all the posts I can find here and they are all a method inside another method, which i'm pretty sure I don't have.

This is the error message it gave me:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression
at rps.Rps.compChoice(Rps.java:63)
at rps.Rps.main(Rps.java:20)

This is my code:

package rps;
import java.util.Random;
import java.util.Scanner;

public class Rps 
{
    Scanner keyboard =new Scanner(System.in);
    Random rand = new Random();
    public static void main(String[] args) {
       
       String compType = "";
       String playerType ="";
       int comp = compChoice ();
       int player = playerChoice ();
       String winLose = choiceTree (player, comp);
       if (comp == 1)
       {
           compType = "Rock.";
       }
       if (comp == 2)
       {
           compType = "Paper.";
       }
       if (comp == 3)
       {
           compType = "Scissors.";
       }
       if (player == 1)
       {
           playerType = "Rock";
       }
       if (player == 2)
       {
           playerType = "Paper";
       }
       if (player == 3)
       {
           playerType = "Scissors";
       }
       System.out.println ("You chose " + playerType + ", and the computer chose " + compType + " " + winLose);
       
       
       }
public static int playerChoice ()
       {
           Scanner keyboard =new Scanner(System.in);
           int choice = 0;
           System.out.print("Rock(1), Paper(2), or Scissors(3)?: ");
           choice = keyboard.nextInt();
       }
public static int compChoice ()
       {
           Random rand = new Random();
           int i = 0;
           i = rand.nextInt(2) + 1;
           return
       }
public static String choiceTree (int player, int comp)
       {
           String answer = " ";
           if (player == 1)
           {
               if (comp == 2)
               {
                   answer = "You lose.";
               }
               if (comp == 3)
               {
                   answer = "You win!";
               }
           }
           if (player == 2)
           {
               if (comp == 1)
               {
                   answer = "You win!";
               }
               if (comp == 3)
               {
                   answer = "You lose.";
               }
           }
           if (player == 3)
           {
               if (comp == 1)
               {
                   answer = "You lose.";
               }
               if (comp == 2)
               {
                   answer = "You win.";
               }
           }
           else
           {
               
           }
       }
}

Recommended Answers

All 6 Replies

public static int compChoice ()
{
Random rand = new Random();
int i = 0;
i = rand.nextInt(2) + 1;
//I've changed the statement below. Change it in your original code
return i;
}

Look at the last line. The return statement had not been properly written. You must return a value.

You had forgotten to return a value here, too! (Below)

public static String choiceTree (int player, int comp)
       {
           String answer = " ";
           if (player == 1)
           {
               if (comp == 2)
               {
                   answer = "You lose.";
               }
               if (comp == 3)
               {
                   answer = "You win!";
               }
           }
           if (player == 2)
           {
               if (comp == 1)
               {
                   answer = "You win!";
               }
               if (comp == 3)
               {
                   answer = "You lose.";
               }
           }
           if (player == 3)
           {
               if (comp == 1)
               {
                   answer = "You lose.";
               }
               if (comp == 2)
               {
                   answer = "You win.";
               }
           }
           else
           {
               
           }
           //look at the line below... Add this to your original code
           return answer;
       }

P.S. I've corrected your code at both of the places! :)

Oh thank god! That totally got rid of the error.
I'm still having one little problem, my random number is always coming out 1, is my random code ok?

NEVERMIND! Fixed it!

public static int compChoice ()
       {
           Random rand = new Random();
           int i = 0;
           i = rand.nextInt(2) + 1;
           return i;
       }

THANKS!!

actually, it would compile and 'work' with just return, the problem was that he didn't end his statement with ;

what do you want your random number to return? between what ranges? or no ranges at all?

I just wanted it to give me a random number between 1 and 3. I fixed it though, here's the code after I fixed it for anyone who searches this post with a similar problem.

public static int compChoice ()
       {
           Random rand = new Random();
           int i = 0;
           rand.nextInt();
           i = rand.nextInt(2) + 1;
           return i;
       }

Thanks you guys for helping me, this was driving me nuts.

    package rps;

        import java.util.Random;
        import java.util.Scanner;

    public class Rps 

    Scanner keyboard = new Scanner(System.in);
    Random rand = new Random();

    public static void main(String[] args) {

        String compType = "";
        String playerType = "";
        int comp = compChoice();
        int player = playerChoice();
        String winLose = choiceTree(player, comp);
        if (comp == 1) {
            compType = "Rock.";
        }
        if (comp == 2) {
            compType = "Paper.";
        }
        if (comp == 3) {
            compType = "Scissors.";
        }
        if (player == 1) {
            playerType = "Rock";
        }
        if (player == 2) {
            playerType = "Paper";
        }
        if (player == 3) {
            playerType = "Scissors";
        }
        System.out.println("You chose " + playerType + ", and the computer chose " + compType + " " + winLose);


    }

    public static int playerChoice() {
        Scanner keyboard = new Scanner(System.in);
        int choice = 0;
        System.out.print("Rock(1), Paper(2), or Scissors(3)?: ");
        choice = keyboard.nextInt();
        return 0;
    }

    public static int compChoice() {
        Random rand = new Random();
        int i = 0;
        i = rand.nextInt(2) + 1;
//I've changed the statement below. Change it in your original code


    package rps;
    import java.util.Random;
    import java.util.Scanner;
    public class Rps 
        Scanner keyboard = new Scanner(System.in);
        Random rand = new Random();

        public static void main(String[] args) {

            String compType = "";
            String playerType = "";
            int comp = compChoice();
            int player = playerChoice();
            String winLose = choiceTree(player, comp);
            if (comp == 1) {
                compType = "Rock.";
            }
            if (comp == 2) {
                compType = "Paper.";
            }
            if (comp == 3) {
                compType = "Scissors.";
            }
            if (player == 1) {
                playerType = "Rock";
            }
            if (player == 2) {
                playerType = "Paper";
            }
            if (player == 3) {
                playerType = "Scissors";
            }
            System.out.println("You chose " + playerType + ", and the computer chose " + compType + " " + winLose);


        }

        public static int playerChoice() {
            Scanner keyboard = new Scanner(System.in);
            int choice = 0;
            System.out.print("Rock(1), Paper(2), or Scissors(3)?: ");
            choice = keyboard.nextInt();
            return 0;
        }

        public static int compChoice() {
            Random rand = new Random();
            int i = 0;
            i = rand.nextInt(2) + 1;
    //I've changed the statement below. Change it in your original code
            return i;
        }

        public static String choiceTree(int player, int comp) {
            String answer = " ";
            if (player == 1) {
                if (comp == 2) {
                    answer = "You lose.";
                }
                if (comp == 3) {
                    answer = "You win!";
                }
            }
            if (player == 2) {
                if (comp == 1) {
                    answer = "You win!";
                }
                if (comp == 3) {
                    answer = "You lose.";
                }
            }
            if (player == 3) {
                if (comp == 1) {
                    answer = "You lose.";
                }
                if (comp == 2) {
                    answer = "You win.";
                }
            } else {
            }
            return null;
        }
    }



    }

    public static String choiceTree(int player, int comp) {
        String answer = " ";
        if (player == 1) {
            if (comp == 2) {
                answer = "You lose.";
            }
            if (comp == 3) {
                answer = "You win!";
            }
        }
        if (player == 2) {
            if (comp == 1) {
                answer = "You win!";
            }
            if (comp == 3) {
                answer = "You lose.";
            }
        }
        if (player == 3) {
            if (comp == 1) {
                answer = "You lose.";
            }
            if (comp == 2) {
                answer = "You win.";
            }
        } else {
        }
        return null;
    }
**

Youlume... did you just solve a question that was solved over a year ago? what's the use of that?

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.