Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. package zad3.napis.liczba.liczba;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7.  
  8. class Dowolna {
  9.  
  10. int indeks = 0;
  11. ArrayList<Przykladowa> lista = new ArrayList<>();
  12.  
  13. void wczytajPlik(String nazwa) {
  14. try {
  15. BufferedReader reader = new BufferedReader(new FileReader(nazwa));
  16. String[] tab;
  17. String line;
  18. while ((line = reader.readLine()) != null) {
  19.  
  20. tab = line.split(" ");
  21.  
  22. lista.add(new Przykladowa(tab[0], Integer.parseInt(tab[1]), Integer.parseInt(tab[2])));
  23. }
  24.  
  25. } catch (Exception e) {
  26. System.out.println("Cos nie tak: " + e);
  27. }
  28.  
  29. }
  30. }
  31.  
  32. class Przykladowa implements Comparable<Przykladowa> {
  33.  
  34. String napis;
  35. int liczba1, liczba2, suma;
  36.  
  37. Przykladowa(String n, int el1, int el2) {
  38. napis = n;
  39. liczba1 = el1;
  40. liczba2 = el2;
  41. suma = liczba1 + liczba2;
  42. }
  43.  
  44. public String toString() {
  45. return "" + napis + " " + liczba1 + " " + liczba2;
  46. }
  47.  
  48. @Override
  49. public int compareTo(Przykladowa o) {
  50. if(suma>o.suma)return 1;
  51. else if(suma<o.suma)return -1;
  52. else return 0;}
  53.  
  54. }
  55.  
  56. public class Zad3NapisLiczbaLiczba {
  57.  
  58. public static void main(String[] args) {
  59. Dowolna d = new Dowolna();
  60. d.wczytajPlik("plik.txt");
  61. Collections.sort(d.lista);
  62. System.out.println(d.lista.get(0));
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement