Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.regex.*;
- import java.lang.Math;
- public class DataMunging {
- public static void main(String[] args) throws IOException {
- BufferedReader in = null;
- String target = null;
- int minDay = 0;
- int minSpread = 100;
- try {
- in = new BufferedReader(new FileReader ("weather.dat"));
- Pattern p = Pattern.compile("^\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
- System.out.println("Pattern: " + p.pattern());
- while (true) {
- target = in.readLine();
- if (target == null)
- break;
- Matcher m = p.matcher(target);
- if (m.find()) {
- int ID = Integer.parseInt(m.group(1));
- int maxT = Integer.parseInt(m.group(2));
- int minT = Integer.parseInt(m.group(3));
- int spread = Math.abs(maxT - minT);
- if (spread < minSpread) {
- minSpread = spread;
- minDay = ID;
- }
- }
- }
- } finally {
- if (in != null) {
- in.close();
- }
- }
- System.out.println("Done parsing file!");
- System.out.println("Smallest spread day is #" + minDay + ", spread is " + minSpread + "°");
- }
- }
Add Comment
Please, Sign In to add comment