Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package main;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- /**
- *
- * @author 37060
- */
- public class Main {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws FileNotFoundException {
- // TODO code application logic here
- ArrayList<Paskaita> pas = new ArrayList();
- nuskaitytiduom("src/data.txt",pas);
- Collections.sort(pas);
- irasytiifaila(pas);
- System.out.println(pas);
- }
- public static void irasytiifaila(ArrayList<Paskaita> as)
- {
- Scanner sc=new Scanner(System.in);
- System.out.println("iveskite semestra");
- int semes=sc.nextInt();
- sc.nextLine();
- System.out.println("Iveskite failo pavadinima");
- String pav=sc.nextLine();
- FileWriter fos=null;
- try {
- fos = new FileWriter(pav);
- for (Paskaita a : as) {
- if (a.semestras == semes)
- {
- fos.write(a.toString() + "\n");
- }
- }
- } catch (Exception e) {
- System.out.println("Klaida");
- } finally {
- try {
- fos.close();
- } catch (Exception e) {
- }
- }
- }
- public static void nuskaitytiduom(String pav,ArrayList<Paskaita> as) {
- Scanner s = null;
- try{
- s = new Scanner(new File(pav));
- String code = null;
- while(s.hasNextLine()){
- code = s.next();
- String pavad =s.next();
- String toliau=s.next();
- int skaicius = 0;
- while(true){
- try{
- skaicius= Integer.parseInt(toliau.trim());
- toliau=s.next();
- break;
- }catch(Exception r){
- pavad=pavad+" "+toliau;
- toliau=s.next();
- }
- }
- int kred=skaicius;
- // toliau=s.next();
- int semes=Integer.parseInt(toliau.trim());
- String destytojas = s.next();
- toliau=s.next();
- destytojas= destytojas+" "+toliau;
- Paskaita paskt = new Paskaita(code,pavad,kred,semes,destytojas);
- as.add(paskt);
- }
- } catch(Exception e){
- e.printStackTrace();
- }finally {
- if (s != null) {
- s.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement