blooming8

Gen ID - Genera

Jun 2nd, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. package genid;
  2.  
  3. import java.io.*;
  4. import java.util.Random;
  5. import javax.swing.JOptionPane;
  6.  
  7. public class Genera {
  8.  
  9.     LineNumberReader lnr;
  10.     public String[] TextArray = {"nomi.txt", "cognomi.txt", "residenza.txt", "statocivile.txt",
  11.         "professione.txt", "capelli.txt", "occhi.txt"}; // nomi dei file da cui si leggono le informazioni
  12.     public int i = 0; // indice del vettore dei file
  13.     public int N_Righe = 0;
  14.  
  15.     public String LeggiDato() {
  16.         Random rand = new Random();
  17.         Contatore cont = new Contatore();
  18.         String dato = "";
  19.  
  20.         try {
  21.             lnr = new LineNumberReader(new FileReader(TextArray[i]));
  22.             N_Righe = cont.ContaRighe(); // numero di righe contato dal metodo ContaRighe()
  23.             int IndexRiga = rand.nextInt(N_Righe - 1) + 1; // numero di riga casuale a cui accedere per leggere
  24.             while (lnr.readLine() != null) {
  25.                 if (lnr.getLineNumber() == IndexRiga) {
  26.                     dato = lnr.readLine();
  27.                 }
  28.             }
  29.             i++; // passa al prossimo file da leggere
  30.         } catch (FileNotFoundException x) {
  31.             JOptionPane.showMessageDialog(null, "File non trovato!", "ERRORE", JOptionPane.ERROR_MESSAGE);
  32.         } catch (IOException x) {
  33.             JOptionPane.showMessageDialog(null, "Problemi tecnici.", "ERRORE", JOptionPane.ERROR_MESSAGE);
  34.         } finally {
  35.             try {
  36.                 if (i == TextArray.length) {
  37.                     lnr.close(); // chiude la risorsa
  38.                 }
  39.             } catch (IOException x) {
  40.                 JOptionPane.showMessageDialog(null, "Problemi tecnici.", "ERRORE", JOptionPane.ERROR_MESSAGE);
  41.             }
  42.         }
  43.         return dato;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment