"Found a { that's missing a matching }" but there is none.

Or at least I can't find any. So this code doesn't seem to work, and it's giving the error stated in the title. I can't find any unbalanced braces Here's my code

ArrayList<String> references; //Names of vars
ArrayList<Object> results; //What the vars equal
ArrayList<ArrayList<String>> tokens; //The tokens
int token; //What token it's on
int line; //The line it's on

void setup() {
  references = new ArrayList<String>();
  results = new ArrayList<Object>();
  tokens = new ArrayList<ArrayList<String>>();
  String[] file = loadStrings("code.sba");
  for(int i = 0; i < file.length; i++) { //Get Tokens
    file[i] += " "; //Make sure there's no missed tokens
    ArrayList<String> temptokens = new ArrayList<String>(); //The tokens in this line
    String currtoken = "";
    boolean instring = false;
    for(int j = 0; j < file[i].length(); j++) {
      char c = file[i].charAt(j); //Get char
      if(c == '"') {
        instring = !instring; //Set that it's inside a string
      }
      if(!instring) {
        if(c != ' ') { //Find a token
          currtoken += c; 
        } else {
          temptokens.add(currtoken);
          currtoken = "";
        }
      } else {
        currtoken += c;
      }
    }
    tokens.add(temptokens); //Add the token that was just found to the tokens in this line
  }
  printArray(tokens);
}
void settings() {
  size(200, 200);
}
void draw() {

}

Answers

  • Edit post, highlight code, press ctrl-o to format.

    Actually, if you hit Ctrl-t in the processing editor it'll indent the code nicely and it'll be easier to see the problem.

  • Ok I fixed the formatting, but all CTRL+t did is move some parenthesis one char to the right, it didn't really make anything that more easier to find

  • IN general it helps a lot

    Also place cursor next to a bracket its partner will be highlighted - helps also to check

    Leave empty lines between functions and after for-loop-sections {}

  • edited March 2018

    I got some questions

    Before setup ()

    ArrayList references;

    should be

          ArrayList <String> references; 
    

    For the others too

    Not sure how to define an ArrayList of ArrayList, maybe easier to capsule the latter one in a small class

  • I know that putting your cursor over a bracket highlights the corresponding bracket, but I can't seem to find any brackets that seem out of place

  • Sorry my post was bad because I write on my phone.

    I corrected it.

    Apologies.

  • Hit F5 to reload page

  • Answer ✓

    The double quote on line 19 might be confusing it.

  • this works though:

    char c = '"';
    
    if (c == '"') {
      println("here");
    }
    
  • line 10 looks suspicious too

    but this test works:

    ArrayList<String> references; //Names of vars
    ArrayList<Object> results; //What the vars equal
    
    ArrayList<ArrayList<String>> tokens; //The tokens
    
    
    void setup() {
    
      tokens = new ArrayList<ArrayList<String>>();
    
      tokens.add ( c1() ); 
    
      printArray(tokens.get(0).toArray());
    }
    
    ArrayList<String> c1() {
    
      ArrayList<String> c_ = new ArrayList();
    
      c_.add("apple");
      c_.add("banana");
      c_.add("pie");
    
      return c_;
    }
    

    I guess we are looking at other things that causes the error besides missing } e.g. wrong ; or so....

  • koogs was right, it's line 19

    you want

         if (c == '\"') {
    

    with \

    apologies, not sure why my mcve did work and yours not

    this works too:

    char c = '\"';
    
    if (c == '\"') {
      println("here");
    }
    
  • what do i win?

  • edited March 2018

    You were right!

    If we were in the same town you'd get a bottle of red wine ;-)

  • edited March 2018

    Well that was the issue, it said earlier that '\"' was an invalid character constant, but now it is valid, but anyways that was the issue

  • I found out by putting huge sections of the code in comments and check if the error still appears to narrow it down

Sign In or Register to comment.