Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a set from a double value from an split string in an array

I have some issues with a current exercise where I have to look up at an array of 1000 strings that are split by "," into siteId, siteName, year,month, day, hour and temperature. The information I need is an integer, which is the day when it was recorded, and a double which is the temperature.

For example,

I get a record from a weather station with the values 3031,LOCH GLACARNOCH SAWS (3031),2015,01,01,18,7.50

So far I have tried to make an arrayList and so with the dates, as there can be no duplicates, but In cannot find the solution to the question: How many days did the temperature fall to 0.0 or below anywhere in the UK?

The year variable is not so important as the records are only from 2015.

String[] weatherData = WeatherData.getData();

    double zeroTemp = 0.0;
    int counter = 0;
    // get the data
            
    
    for (int i = 1; i <= weatherData.length-1; i++) {
        
        String line = weatherData[i];
        String[] data = line.split(",");
        int date = Integer.parseInt(data[6]);
        int month = Integer.parseInt(data[5]);
        int year = Integer.parseInt(data[4]);
        double temp = Double.parseDouble(data[9]);
            
        LocalDate when = LocalDate.of(year, month, date);
        
        for(int j = 1; j<= data.length; j++) {
        HashSet<LocalDate> duplicateDates = new HashSet<>(Arrays.asList(when));
        System.out.println(when);
          duplicateDates.add(when);
        
        System.out.println(duplicateDates.size());
        }
        if(zeroTemp >= temp) { 
            counter++;      
            }
        }
        System.out.println("The temperature fell to 0.0 degrees or below in " + counter  + " days.");
}
like image 884
AxelCagira Avatar asked Mar 11 '26 00:03

AxelCagira


1 Answers

The information I need is an integer, which is the day when it was recorded

Not really.

Either the input may contain multiple entries for a single date, in which case you need to know the entire date (even if year is irrelevant, there's month + day; you can't just look at that 'day' value, or you'd consider 1st of March and 1st of April as the same day which is clearly incorrect.

Or, there are no duplicates: For any given exact day, there can only ever be a single record. This sounds wrong (given the "or below anywhere in the UK?" part), but if so, you don't need to look at day or month whatsoever; just count the # of entries with a temperature recording below 0.

You could hack it and turn the month and day fields into a single unique integer (multiply month by 32, then add days: That guarantees you a unique 'month+day' ID value, because no month has 32 days, overlap is not possible). Or write it properly and turn that year+month+day field into a LocalDate instance: LocalDate when = LocalDate.of(2015, 1, 1) would produce a LocalDate instance representing jan 1st 2015.

ArrayList isn't a good tool for uniqueness; you're probably looking for HashMap which maps a given 'key' to a given value, or better yet, a HashSet, which stores values, but which will not store the same value more than once. You can then add any LocalDate for which you have found a record at sub-zero, and if you then find another such record, you'd not change anything - just add the date to the set a second time, as it won't have any effect. Then at the very end just check how large the set is. So:

  1. Create a HashSet of LocalDate.
  2. For each record: Write code that turns the year, month, and day values into a LocalDate, using LocalDate.of(year, month, day).
  3. Check if the temperature value is below 0.0. If so, add the date you made to the set.
  4. When done with the loops, the size of the set represents the # of days that the temperature was below 0 anywhere in the UK.

NB: line.split(",") will take a line of text and return a string array by splitting the input line at each comma. Integer.parseInt("50") returns the number 50.

This should be all the info you need to finish your homework.

like image 152
rzwitserloot Avatar answered Mar 12 '26 14:03

rzwitserloot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!