hao
hao

Reputation: 29

Java call method

import java.util.HashMap;
   class Room1 {
       private String description;
       private HashMap<String, Room1> dir = new HashMap<String, Room1>();
       Room1(String de) {
           description = de;
       }
       public String toString() {
           return description;
       }
       public void add(String s, Room1 r) {
           dir.put(s, r);
       }
   }
   class Game {
       Room1 lobby = new Room1("lobby");
       Room1 pub = new Room1("pub");
       lobby.add("one", pub); //syntax error
   }

When i call the add method.the eclipse tell me there are errors existing.i'm confused.i can't find the problem.

Upvotes: 0

Views: 87

Answers (3)

Ramath
Ramath

Reputation: 188

Wrap the code in a method .

class Game {
   Room1 lobby = new Room1("lobby");
   Room1 pub = new Room1("pub");

   public void init(){
        lobby.add("one", pub); //syntax error
   }    
}

Upvotes: 1

Sasi Kumar
Sasi Kumar

Reputation: 13348

Use correct syntax

public class testing {
public static void main(String arg[]) {

    Room1 lobby = new Room1("lobby");
    Room1 pub = new Room1("pub");
    lobby.add("one", pub);
  } 
 }

Upvotes: 0

Lorenz K.
Lorenz K.

Reputation: 21

You must call the methods in a function.

class Game {
    Room1 lobby = new Room1("lobby");
    Room1 pub = new Room1("pub");
    public Game() {
        lobby.add("one", pub);
    }
}

Upvotes: 2

Related Questions