Tiina Tiina - 9 months ago 76
Java Question

Using Scanner with hashmap


  1. Reading from
    JSON
    file.

  2. Using
    Scanner
    so that the user can give an input (time).

  3. Using for loop to compare the input value with the
    hashmap
    if it matches any value in the hash-map then the code prints all the related values with that time.



The result shows out for me
NULL


How to get the correct values ?

If type in
Scanner
2008-09-18 13:15:01
I get
7.64
1.89
30.10


Class Main:

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 );

}
}

}

Answer Source

@ΦXocę 웃 Пepeúpa ツ is right. The actual problem is the " 2008-09-18 13:15:01 " spaces around the time stamp. you need to trim() them before mapping. And as you provided your source code, I found several problems and rewrite them.

  • First of all the class name Main is confusing for the IDE. Rename the class name to something different eg. WeatherApp
  • Rethink about your Mesaurement class. Its needs to be a simple Plain Old Java Object (POJO). you could search google what is POJO and will find a lot of details.

Consider the refactored code I did in this case.

public class Measurement {

    Float temp;
    Float wind;
    Float humid;

    public Measurement() {
    }

    public Measurement(String d, Float t, Float w, Float h) {
        this.temp = t;
        this.wind = w;
        this.humid = h;
    }

    public Float getTemp() {
        return temp;
    }

    public void setTemp(Float temp) {
        this.temp = temp;
    }

    public Float getWind() {
        return wind;
    }

    public void setWind(Float wind) {
        this.wind = wind;
    }

    public Float getHumid() {
        return humid;
    }

    public void setHumid(Float humid) {
        this.humid = humid;
    }

    public void print(String time) {
        System.out.println("The detailed info : " + time + "-" + this.temp + " " + this.wind + "-" + this.humid);
    }
}

And finally your Main class is refactored as below:

public class WeatherApp {

    public static void main(String[] args) throws ParseException, IOException {

        FileReader fr = new FileReader("/jsontest/src/main/java/json/test/weather.json");
        JSONParser jp = new JSONParser();
        JSONObject jo = (JSONObject) jp.parse(fr);
        JSONArray ja = (JSONArray) jo.get("measurements");

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

        for (Object json : ja) {

            JSONObject jsonElement = (JSONObject) json;
            String time = jsonElement.get("time").toString().trim();

            Measurement measurement = new Measurement();
            measurement.setTemp(Float.valueOf(jsonElement.get("temp").toString()));
            measurement.setWind(Float.valueOf(jsonElement.get("wind").toString()));
            measurement.setHumid(Float.valueOf(jsonElement.get("humid").toString()));
            measurements.put(time, measurement);
        }

        System.out.println("");
        System.out.println("Please enter time:");

        Scanner scan = new Scanner(System.in);
        String timeQuery = scan.nextLine();

        Measurement output = measurements.get(timeQuery);
        output.print(timeQuery);
    }
}

And certainly you can modify your code as you like and make things do. but getting a better OOP concept is necessary.