user2840682 user2840682 - 4 months ago 9
Java Question

Why is there the compile error, " ; expected " for my Java Code?

I really do not understand why I am getting this compile error after the parentheses for

"Map<String, Integer> buildTable(){"
.

Here is the code I am working on: I already have the city class defined.

import java.util.Map;
import java.util.HashMap;

public class CityMap{

public static void main(String[] args){
String _city;
Map<String, Integer> cityTable = buildTable();

Map<String, Integer> buildTable(){
String aCity;
Map<String, Command> result = new HashMap<String, Command>();

aCity = new City();
result.put("NYC", 100000);


aCity = new City();
result.put("Boston", 500);

return result;
}


I am a beginner, so any explanation is welcome.

Answer

Your method declaration for buildTable needs to live outside of your method declaration for main.

I.E.,

   import java.util.Map;
   import java.util.HashMap;

public class CityMap{

public static void main(String[] args)
{  
  String _city;
  Map<String, Integer> cityTable = buildTable();
}

public static Map<String, Integer> buildTable(){
 String aCity;
 Map<String, Command> result = new HashMap<String, Command>();

 aCity = new City();
 result.put("NYC", 100000);


 aCity = new City();
 result.put("Boston", 500);

  return result;
  }
}