user2152229
user2152229

Reputation: 13

Make a New Double from Hashmap Values

I'm getting the following error :

cannot find symbol, symbol: variable values, locations variable planet of type Map< String,Double >

on this line Double[] Total = new Double [planet.values];

Here is my code :

public static void main(String[] args) {

    Map<String, Double> planet = new HashMap();

    Double Weight = 205.00;

    planet.put("Earth", 1.00);
    planet.put("Moon", .378);
    planet.put("Mercury", .166);
    planet.put("Jupiter", 2.364);
    planet.put("Venus", .907);
    planet.put("Uranus", .889);

    System.out.println("Amount of gravity on each planet: " + planet + "\n");  


    Double[] Total = new Double [planet.values];
    System.out.println(planet.values());

Upvotes: 0

Views: 62

Answers (2)

Amir Pashazadeh
Amir Pashazadeh

Reputation: 7302

Just change it to planet.values() instead of 'planet.values', you are trying to access the field not calling the method.

So this will be your code:

Collection<Double> values = planet.values();
Double[] total = values.toArray(new Double[values.size()]);

By the way, an array's toString() method does not render values. If you want to write the values to console use Arrays.toString(array).

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691765

Map doesn't have any public field named value. So planet.value is invalid. If you want to initialize an array which has a length equal to the size of the map, use size():

Double[] total = new Double[planet.size()];

Also, please respect tha Java naming conventions. Variables start with a lowercase letter.

Upvotes: 1

Related Questions