Advertisement
Guest User

main

a guest
Nov 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package main;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileWriter;
  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13. import java.util.Scanner;
  14.  
  15. /**
  16. *
  17. * @author 37060
  18. */
  19. public class Main {
  20.  
  21. /**
  22. * @param args the command line arguments
  23. */
  24. public static void main(String[] args) throws FileNotFoundException {
  25. // TODO code application logic here
  26. ArrayList<Paskaita> pas = new ArrayList();
  27. nuskaitytiduom("src/data.txt",pas);
  28. Collections.sort(pas);
  29. irasytiifaila(pas);
  30. System.out.println(pas);
  31.  
  32. }
  33. public static void irasytiifaila(ArrayList<Paskaita> as)
  34. {
  35. Scanner sc=new Scanner(System.in);
  36. System.out.println("iveskite semestra");
  37. int semes=sc.nextInt();
  38. sc.nextLine();
  39. System.out.println("Iveskite failo pavadinima");
  40. String pav=sc.nextLine();
  41. FileWriter fos=null;
  42. try {
  43. fos = new FileWriter(pav);
  44. for (Paskaita a : as) {
  45. if (a.semestras == semes)
  46. {
  47.  
  48. fos.write(a.toString() + "\n");
  49. }
  50. }
  51. } catch (Exception e) {
  52. System.out.println("Klaida");
  53. } finally {
  54. try {
  55. fos.close();
  56. } catch (Exception e) {
  57. }
  58. }
  59. }
  60.  
  61. public static void nuskaitytiduom(String pav,ArrayList<Paskaita> as) {
  62. Scanner s = null;
  63.  
  64. try{
  65. s = new Scanner(new File(pav));
  66. String code = null;
  67. while(s.hasNextLine()){
  68. code = s.next();
  69. String pavad =s.next();
  70. String toliau=s.next();
  71. int skaicius = 0;
  72. while(true){
  73. try{
  74. skaicius= Integer.parseInt(toliau.trim());
  75. toliau=s.next();
  76. break;
  77. }catch(Exception r){
  78. pavad=pavad+" "+toliau;
  79. toliau=s.next();
  80.  
  81. }
  82. }
  83. int kred=skaicius;
  84. // toliau=s.next();
  85. int semes=Integer.parseInt(toliau.trim());
  86. String destytojas = s.next();
  87. toliau=s.next();
  88. destytojas= destytojas+" "+toliau;
  89. Paskaita paskt = new Paskaita(code,pavad,kred,semes,destytojas);
  90. as.add(paskt);
  91.  
  92. }
  93. } catch(Exception e){
  94. e.printStackTrace();
  95. }finally {
  96. if (s != null) {
  97. s.close();
  98. }
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement