简体   繁体   中英

Hashtable “Unresolved compilation” in java

this is my class

package main;

import java.util.Hashtable;

public class Information {

    private final static String name = "Info Name";
    private final static String direccion = "direction street number";

    Hashtable<String,String> coordenates = new Hashtable<String,String>();
    coordenates.put("lat", "36.564565465");
    coordenates.put("lng", "64.584616511");

}

i have learned from oracle documentation

This example creates a hashtable of numbers. It uses the names of the numbers as keys:

   Hashtable<String, Integer> numbers
     = new Hashtable<String, Integer>();
   numbers.put("one", 1);
   numbers.put("two", 2);
   numbers.put("three", 3);
To retrieve a number, use the following code:

   Integer n = numbers.get("two");
   if (n != null) {
     System.out.println("two = " + n);
   }

what is wrong with my code...

im using Eclipse for Java EE Developers, Java SE SDK 7 (1.7) on windows 7 64bits

thanks!

You're trying to execute code out side of the context of a method, constructor or static initialiser...

public class Information {

    private final static String name = "Info Name";
    private final static String direccion = "direction street number";

    Hashtable<String,String> coordenates = new Hashtable<String,String>();
    /**
      This belongs in a constructor or method
      coordenates.put("lat", "36.564565465");
      coordenates.put("lng", "64.584616511");
    **/

}

您正在尝试在方法外调用hashtable.put(),则可以在构造函数内进行操作,我不确定您的要求是什么,但我想您可以避免hashtable的开销并定义两个字段lat和long

1.Write it in a main function

public static void main(String args[]) {    
    Hashtable<String,String> coordenates = new Hashtable<String,String>();
    coordenates.put("lat", "36.564565465");
     coordenates.put("lng", "64.584616511");    
}

2.write it in Constructor

    Information() {
      access setter of coordinates to put values
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM