1,105,578 Community Members

Illegal start of expression error. (Not a method inside another method)

Member Avatar
Phrren
Newbie Poster
3 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
           {
               
           }
       }
}
Member Avatar
dantinkakkar
Junior Poster
174 posts since Aug 2011
Reputation Points: 19 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
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;
}

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! :)

Member Avatar
Phrren
Newbie Poster
3 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!!

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
1
 

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?

Question Answered as of 2 Years Ago by stultuske and dantinkakkar
Member Avatar
Phrren
Newbie Poster
3 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
youlume
Newbie Poster
1 post since Jul 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 
    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;
    }
**
Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
1
 

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

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: