Question about using Hashmap

Asked

Viewed 31 times

-2

I have a question about the use of Hashmap of Java. On this occasion, I have a class with the attribute Hashmap custo.

In this class, I need to create a builder and in this builder, I need to request the insertion of 3 integers that will compose my cost (gold, wood and diamond).

My question is: how do I request this 3 data in order to fill them in my cost? And how do I display and use the same later?

  • https://answall.com/a/72885/28595

  • 1

    Requesting means asking the user to enter the information. If you didn’t explain the input, then assume it is from the default input (usually the keyboard). If your problem is this, you have no problems with the HashMap but with user interaction

  • I need to get him to type in this 3 information and I need to store it, however, I don’t know how to do this using Hashmap.

1 answer

-1


I believe that’s what you want.

Hugs

    //CRIANDO O MAP CUSTO
    HashMap<String, Integer> custo = new HashMap<String, Integer>();

    //PEDINDO A INFORMAÇÃO PARA O USUÁRIO
    System.out.println("DIGITE A QUANTIDADE DE OURO");
    Scanner readOuro = new Scanner(System.in);

    //OBTENDO A INFORMAÇÃO DO TECLADO
    Integer qtdOuro = readOuro.nextInt();

    //INSERINDO A QUANTIDADE DE OURO AO MAPA COM A CHAVE OURO
    custo.put("OURO", qtdOuro);


    //FAZENDO A MESMA COISA PARA MADEIRA E DIAMANTE

    System.out.println("DIGITE A QUANTIDADE DE MADEIRA");
    Scanner readMadeira = new Scanner(System.in);
    Integer qtdMadeira = readMadeira.nextInt();
    custo.put("MADEIRA", qtdMadeira);




    System.out.println("DIGITE A QUANTIDADE DE DIAMANTE");
    Scanner readDiamante = new Scanner(System.in);
    Integer qtdDiamante = readDiamante.nextInt();
    custo.put("DIAMANTE", qtdDiamante);
  • Perfect! Can I only pass 2 information at a time or can I create just one put to pass all at once? I ask this because my idea is to insert everything directly in the constructor that I will create of this class.

  • Hasmap is Key, Value. Keeping this in mind you can model your problem in many ways, this was the most intuitive I found to show you...

  • Helped me a lot already! Thank you very much. Big hug! : D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.