JCF - Studente.java
Cerca
 











Studente.java

Caricato da: Bonny
Scarica il programma completo

  1. package esameinfo;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.util.ArrayList;
  6.  
  7. public class Studente implements Comparable<Studente> {///
  8.  
  9.     String nome, cognome;
  10.     int metr;
  11.  
  12.     public Studente(String c, String n, int m) {
  13.  
  14.         this.cognome = c;
  15.         this.nome = n;
  16.         this.metr = m;
  17.     }
  18.  
  19.     public int compareTo(Studente x) {
  20.  
  21.         Studente p = (Studente) x;
  22.  
  23.         return cognome.compareTo(p.cognome);
  24.     }
  25.  
  26.     @Override
  27.     public String toString() {
  28.  
  29.         return (this.cognome + " " + this.nome + "  matr " + String.valueOf(metr));
  30.     }
  31.  
  32.     public static void main(String[] args) throws IOException {
  33.  
  34.         InputStreamReader in = new InputStreamReader(System.in);
  35.         BufferedReader tast = new BufferedReader(in);
  36.  
  37.         List<Studente> stud = new ArrayList<Studente>();
  38.        
  39.         String n, c, m;
  40.  
  41.         while (true) {
  42.  
  43.             System.out.println("inserire il cognome");
  44.             c = tast.readLine();
  45.  
  46.             if (c.equals("")) {
  47.                 break;
  48.             } else {
  49.  
  50.                 System.out.println("\ninserire il nome");
  51.                 n = tast.readLine();
  52.                 System.out.println("\ninserire matricola");
  53.                 m = tast.readLine();
  54.  
  55.                 Studente x = new Studente(c, n, Integer.parseInt(m));
  56.                 stud.add(x);
  57.             }
  58.  
  59.         }
  60.         System.out.println(stud);
  61.  
  62.         Collections.sort(stud);
  63.  
  64.         System.out.println(stud);
  65.  
  66.         FileWriter f = new FileWriter("out.txt");
  67.         PrintWriter fout = new PrintWriter(f);
  68.  
  69.         Studente v[] = (Studente[]) stud.toArray(new Studente[stud.size()]);////
  70.  
  71.         for (int i = 0; i < v.length; i++) {
  72.             fout.println(v[i].toString());
  73.         }
  74.         fout.close();
  75.  
  76.         FileWriter f2 = new FileWriter("out2.txt");
  77.         PrintWriter fout2 = new PrintWriter(f2);
  78.         for (Object x : stud) {
  79.             fout2.println(x);
  80.             System.out.println(x);
  81.         }
  82.         fout2.close();
  83.     }
  84. }
 

Creative Commons License
Il layout di questo sito è concesso sotto licenza Creative Commons.
Per maggiori informazioni sulle licenze dei contenuti del sito, clicca.