Search code examples
javadebuggingidentifier

Java - <identifier> required/illegal start of expression


I'm running into some trouble with a Java assignment. I'm not sure what I'm doing wrong, but it's telling me that I need an identifier and that the lines are an illegal start of an expression when I try to add() to an ArrayList.

Here's my code, the offender starts at printStrat.add("1. Tit-For-Tat\n"); :

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.lang.Math;

public class PDGame
{
  private static ArrayList<boolean> rcrd = new ArrayList();
  private static Gamestat globalStats = new Gamestat();
  private String[] rslt = new String[] {
     "You and your partner remain silent\n", 
     "Your partner testifies against you and you remain silent\n",
     "You testify against your partner and he remains silent\n",
     "You both testify against each other\n" };
  private ArrayList<String> printStrat = new ArrayList<String>();
  printStrat.add("1. Tit-For-Tat\n");
  printStrat.add("2. Tit-For-Two-Tats\n");
  printStrat.add("3. Tit-For-Tat with forgiveness\n"); 
  private int strat = 1;
  private int npcPrison = 0;
  private int pcPrison = 0;

  public PDGame() 
  {
    //there is no need for a scanner to read if I won't be doing
    // anything with it, so I did not implement it
  }

  public String playRound(int decision)
  {
    boolean myMove;
    if(strat == 1) { //Tit-for-Tat
      if(rcrd.size() < 1) //if first move
        myMove = false; 
      else //set npc move to last player's move
        myMove = rcrd.get(rcrd.size()-1);
    }
    else if(strat == 2) { //Tit-for-Two-Tats
      if(rcrd.size() < 2)  //if first move
        myMove = false; 
      else if(rcrd.get(rcrd.size()-1) == true && rcrd.get(rcrd.size()-2) == true)
        myMove = true; //if player betrayed last two times, betray
      else //otherwise, forgive
        myMove = false; 
    }
    else if(strat == 3) { 
      int ran = (int)(Math.random()*100+1);
      if(rcrd.size() < 1) //if first move,forgive
        myMove = false; 
      else if(ran < 50) //if ran > 75, act normally
        myMove = rcrd.get(rcrd.size()-1);
      else //if ran = 1-50, forgive
        myMove = false;
    }

    if(decision == 1) {
      if(myMove == false) {
        result = rslt[0];
        npcPrison = 2;
        pcPrison = 2;
      }
      else if(myMove == true) {
        result = rslt[1];
        npcPrison = 1;
        pcPrison = 5;
      }
      rcrd.add(false);
    }
    else if(decision == 2) {
      if(myMove == false) {
        result = rslt[2];
        npcPrison = 5;
        pcPrison = 1;
      }
      else {
        result = rslt[3];
        npcPrison = 3;
        pcPrison = 3;
      }
      rcrd.add(true);
    } 
    globalStats.update(pcPrison,npcPrison);
    globalStats.setDate();
    return result;
  }

  public ArrayList<String> getStrategies() 
  {
    return printStrat;
  }

  public void setStrategy(int strategy) 
  {
    strat = strategy;
    globalStats.setStrategy(strategy);
  }

  public GameStat getStats() 
  {
    return globalStats();
  }

  public String getScores() 
  {
    String scores = "Your prison sentence is: " + pcPrison + "\n";
    scores += "Your partner's prison sentence is " + npcPrison + "\n";
    return scores;
  }
}

Any and all help is appreciated.


Solution

  • You cannot put code (except assignments) straight under a class declaration - it should be put in a method, constructor or anonymous block. One way to solve this would be to move your calls to printStrat.add to the constructor:

    public class PDGame
    {
      /* snipped */
    
      private ArrayList<String> printStrat = new ArrayList<String>();
    
      public PDGame() 
      {
        printStrat.add("1. Tit-For-Tat\n");
        printStrat.add("2. Tit-For-Two-Tats\n");
        printStrat.add("3. Tit-For-Tat with forgiveness\n"); 
      }
    
      /* snipped */
    }