使用扫描程序在哈希映射中搜索密钥

时间:2018-03-20 13:03:06

标签: java hashmap java.util.scanner

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


public class MyMap {
    public static void main (String[] args) {

        Map<String, Car> cars = new HashMap<> ();
        cars.put ("ID1", new Car("Dave", "LT12 DDS"));
        cars.put ("ID2", new Car("Steve", "GB14 HHG"));
        cars.put ("ID3", new Car("Molly", "LT18 SDF"));


        System.out.println ("Car with ID1 is " + cars.get ("ID1"));



    }
}

我可以在不使用扫描仪类的情况下从地图中获取详细信息。 我想让用户输入ID并从HashMap中获取结果

class Car {
    public Car (String name, String barcode) {
        this.name = name;
        this.barcode = barcode;
    }

    public String toString () {
        return "Car: " + name + " (" + barcode + ")";
    }

    public final String name;
    public final String barcode;
}

这一直有效,直到我尝试添加扫描仪部件。我希望用户输入ID,然后输入要从哈希地图中检索的结果

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class MyMap {
    public static void main (String[] args) {

        Map<String, Car> cars = new HashMap<> ();
        cars.put ("ID1", new Car("Dave", "LT12 DDS"));
        cars.put ("ID2", new Car("Steve", "GB14 HHG"));
        cars.put ("ID3", new Car("Molly", "LT18 SDF"));

        Scanner ab=new Scanner(System.in);

        System.out.println("Enter ID: ");
        int id=ab.nextInt();




          //user input should get details from HashMap??
        System.out.println ("Car with ID1 is " + cars.get (int id));

        //System.out.println ("Car with ID1 is " + cars.get ("ID1"));

    }
}


//the Car class

class Car {
    public Car (String name, String barcode) {
        this.name = name;
        this.barcode = barcode;
    }

当用户输入提供ID号时,我正在努力获取Hash Map以获取详细信息。

2 个答案:

答案 0 :(得分:0)

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

public class MyMap {

    enter code here
        public static void main (String[] args) {

            Map<String, Car> cars = new HashMap<>();
            cars.put ("ID1", new Car("Dave", "LT12 DDS"));
            cars.put ("ID2", new Car("Steve", "GB14 HHG"));
            cars.put ("ID3", new Car("Molly", "LT18 SDF"));

            Scanner ab=new Scanner(System.in);

            System.out.println("Enter ID: ");
            String id=ab.next();

            System.out.println ("Car with ID1 is " + cars.get (id));



        }
    }

答案 1 :(得分:0)

您的地图 <?php while ($row = mysqli_fetch_array($cr_results)) { if ($row['name'] == "pr_created") { echo "createViewCard('Purchase Requisition', 'veiwpr');"; } if ($row['name'] == "ba_created") { echo "createViewCard('Business Advance Form', 'veiwba');"; } if ($row['name'] == "cpr_created") { echo "createViewCard('Cheque/Payment Requisition', 'veiwcpr');"; } if ($row['name'] == "cqc_created") { echo "createViewCard('Comparison Quotation Chart', 'veiwcqc');"; } if ($row['name'] == "pc_created") { echo "createViewCard('Petty Cash', 'veiwpc');"; } } ?> 的类型为cars 因此,要从地图中获取汽车,您需要以Map<String, Car>为参数调用cars.get

而不是调用String使用ab.nextInt()。这会将完整的输入行返回ab.nextLine(),您可以将其用作地图的关键字。

String

要完美打印您的汽车,请覆盖Map<String, Car> cars = new HashMap<> (); cars.put ("ID1", new Car("Dave", "LT12 DDS")); cars.put ("ID2", new Car("Steve", "GB14 HHG")); cars.put ("ID3", new Car("Molly", "LT18 SDF")); Scanner ab=new Scanner(System.in); System.out.println("Enter ID: "); String inputedID = ab.nextLine(); System.out.println("Car with " + inputedId + " is " + cars.get(inputedId)); 的{​​{1}}方法,如shahaf的回答:

  

toString