Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Hashmap中搜索_Java_Json_Hashmap_Java.util.scanner - Fatal编程技术网

Java 在Hashmap中搜索

Java 在Hashmap中搜索,java,json,hashmap,java.util.scanner,Java,Json,Hashmap,Java.util.scanner,使用for循环将输入值与hashmap进行比较,如果输入值与hashmap中的任何值匹配,则代码将在该时间打印所有相关值 结果为我显示NULL System.out.println("Please enter time :"); Scanner scan = new Scanner(System.in); String value = scan.nextLine();//Read input-time Measurement meas

使用for循环将输入值与
hashmap
进行比较,如果输入值与hashmap中的任何值匹配,则代码将在该时间打印所有相关值

结果为我显示
NULL

       System.out.println("Please enter time :");
        Scanner scan = new Scanner(System.in);
        String value = scan.nextLine();//Read input-time
        Measurement measurement = measurements.get(value);//there can only be 1 Measurement for 1 time
        if(measurement != null){
            System.out.println(measurement);
        }}
等级测量:

    public void getTimeInfo(String value)
    {

        value = Measurements.get(time);
        if (value == null) {
            throw new MeasurementException();
        }

        System.out.println("The detailed info : " + this.time + "-" + this.temp+ " "+ this.wind+ "-" + this.humid );

    }
    }

}
按照您提到的3个步骤(忽略Json部分)并重用部分代码,我可以为您提供以下代码:

Main.java

public class Main {

    static HashMap<String, Measurement> measurements = new HashMap();

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {//Create 3 measurements
            String time = ""+i;
            measurements.put(time, new Measurement(time, (float) i, (float) i, (float) i));
        }

        System.out.println("Please enter time :");
        Scanner scan = new Scanner(System.in);
        String value = scan.nextLine();//Read input-time
        Measurement measurement = measurements.get(value);//there can only be 1 Measurement for 1 time
        if(measurement != null){
            System.out.println(measurement);
        }

    }
}
public class Measurement {
    String time ;
    Float temp;
    Float wind;
    Float humid;
    int iD;   

    public Measurement(String d, Float t, Float w, Float h){
        this.time = d;

        this.temp = t;
        this.wind = w;
        this.humid = h;
    }

    @Override
    public String toString() {
        return "The detailed info : " + this.time + "-" + this.temp+ " "+ this.wind+ "-" + this.humid;
    }
}
它可能不完全符合您的需要,但可以提供帮助。

遵循您提到的3个步骤(忽略Json部分)并重用一些代码,我可以为您提供以下代码:

Main.java

public class Main {

    static HashMap<String, Measurement> measurements = new HashMap();

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {//Create 3 measurements
            String time = ""+i;
            measurements.put(time, new Measurement(time, (float) i, (float) i, (float) i));
        }

        System.out.println("Please enter time :");
        Scanner scan = new Scanner(System.in);
        String value = scan.nextLine();//Read input-time
        Measurement measurement = measurements.get(value);//there can only be 1 Measurement for 1 time
        if(measurement != null){
            System.out.println(measurement);
        }

    }
}
public class Measurement {
    String time ;
    Float temp;
    Float wind;
    Float humid;
    int iD;   

    public Measurement(String d, Float t, Float w, Float h){
        this.time = d;

        this.temp = t;
        this.wind = w;
        this.humid = h;
    }

    @Override
    public String toString() {
        return "The detailed info : " + this.time + "-" + this.temp+ " "+ this.wind+ "-" + this.humid;
    }
}

它可能不完全适合您的需要,但可以提供帮助。

您的代码在您创建的哈希映射中找不到。它在另一个HashMap中查找,该HashMap存储在您创建的虚拟
c1
度量中。为什么要创建此
c1
测量?为什么测量值包含测量图?从度量类中删除该映射。JB Nizet,从度量类中删除映射后仍将提供NULL。您的代码不会在您创建的HashMap中查找。它在另一个HashMap中查找,该HashMap存储在您创建的虚拟
c1
度量中。为什么要创建此
c1
测量?为什么测量值包含测量图?从Measurement类中删除该映射。如果从Measurement类中删除映射,则该映射仍为NULL。。