Guest User

Untitled

a guest
Jan 23rd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.regex.*;
  3. import java.lang.Math;
  4.  
  5. public class DataMunging {
  6.  
  7. public static void main(String[] args) throws IOException {
  8. BufferedReader in = null;
  9. String target = null;
  10. int minDay = 0;
  11. int minSpread = 100;
  12.  
  13. try {
  14. in = new BufferedReader(new FileReader ("weather.dat"));
  15. Pattern p = Pattern.compile("^\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
  16. System.out.println("Pattern: " + p.pattern());
  17.  
  18. while (true) {
  19. target = in.readLine();
  20.  
  21. if (target == null)
  22. break;
  23.  
  24. Matcher m = p.matcher(target);
  25. if (m.find()) {
  26. int ID = Integer.parseInt(m.group(1));
  27. int maxT = Integer.parseInt(m.group(2));
  28. int minT = Integer.parseInt(m.group(3));
  29.  
  30. int spread = Math.abs(maxT - minT);
  31.  
  32. if (spread < minSpread) {
  33. minSpread = spread;
  34. minDay = ID;
  35. }
  36. }
  37. }
  38.  
  39. } finally {
  40. if (in != null) {
  41. in.close();
  42. }
  43. }
  44.  
  45. System.out.println("Done parsing file!");
  46. System.out.println("Smallest spread day is #" + minDay + ", spread is " + minSpread + "°");
  47. }
  48. }
Add Comment
Please, Sign In to add comment