CathyLu
CathyLu

Reputation: 757

What's wrong with this error? ----Syntax error on token ";", { expected after this token

Syntax error on token ";", { expected after this token. 

I got this error on the 11th line and 19th line. Is there anyone can tell me what's the problem with it?

import java.util.*;
class veding_machine{
    State st;
    veding_machine vm;
    private int price;
    private int k;
    private int k1;
    private int t;
    private int s;
    State Ls[]=new State[7]; // 11th line

    Ls[0]=idle;
    Ls[1]=coins_inserted;
    Ls[2]=sugar;
    LS[3]=nsc;
    Ls[4]=nlc;
    Ls[5]=et;
    Ls[6]=st; // 19th line

 public veding_machine(){        k=0;        k1=0;       t=0;        price=0;
            }

    public void setK(int k){
         this.k=k;
    }

Upvotes: 0

Views: 4826

Answers (3)

Hasen
Hasen

Reputation: 340

State Ls[]=new State[7]; // 11th line

This is an instantiated array. You should place it in the constructor or initialization block. Also it is better to init the reference in the Java way:

State[] Ls = new State[7];

This

LS[3]=nsc;

should be

Ls[3]=nsc;

And again init those elements in the constructor or init block.

Also, I know this doesn't answer the question but I just have to say something about naming and style. Please name vending_machine class like VendingMachine and give the other objects more descriptive names instead of vague letters. Following convention helps when you come back to your code a week/month/year later and saves you from trying to figure out what the hell you were doing. Even if it is just a little project in school that is where it matters most that you learn and practice these conventions.

Also, it's nice when code is aesthetically pleasing.

Upvotes: 0

Abimaran Kugathasan
Abimaran Kugathasan

Reputation: 32468

Initialize that array inside a Constructor, you can't initialize them like that, initialize them when you declare the array, or in a Constructor or in a initialization block. And correct the spelling mistake. Have look on this tutorial.

Ls[0]=idle;
Ls[1]=coins_inserted;
Ls[2]=sugar;
Ls[3]=nsc;
Ls[4]=nlc;
Ls[5]=et;
Ls[6]=st

;

Upvotes: 1

Neil Essy
Neil Essy

Reputation: 3607

The initialization of Ls should be inside the vending_machine constructor and should be creating instances of the classes "idle" and "coins_inserted", etc...

Ls[0] = new idle();
Ls[1] = new coins_inserted();
Ls[2] = new sugar();
Ls[3] = new nsc();
Ls[4] = new nlc();
Ls[5] = new et();
Ls[6] = new st();

and these classes need to extend the State class

class idle extends State {
  // ...
}

They don't need a state instance inside them.

    // removed, State st;
    public coins_inserted(){
      // removed, st=new State();
    }

Upvotes: 0

Related Questions