Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package genid;
- import java.io.*;
- import java.util.Random;
- import javax.swing.JOptionPane;
- public class Genera {
- LineNumberReader lnr;
- public String[] TextArray = {"nomi.txt", "cognomi.txt", "residenza.txt", "statocivile.txt",
- "professione.txt", "capelli.txt", "occhi.txt"}; // nomi dei file da cui si leggono le informazioni
- public int i = 0; // indice del vettore dei file
- public int N_Righe = 0;
- public String LeggiDato() {
- Random rand = new Random();
- Contatore cont = new Contatore();
- String dato = "";
- try {
- lnr = new LineNumberReader(new FileReader(TextArray[i]));
- N_Righe = cont.ContaRighe(); // numero di righe contato dal metodo ContaRighe()
- int IndexRiga = rand.nextInt(N_Righe - 1) + 1; // numero di riga casuale a cui accedere per leggere
- while (lnr.readLine() != null) {
- if (lnr.getLineNumber() == IndexRiga) {
- dato = lnr.readLine();
- }
- }
- i++; // passa al prossimo file da leggere
- } catch (FileNotFoundException x) {
- JOptionPane.showMessageDialog(null, "File non trovato!", "ERRORE", JOptionPane.ERROR_MESSAGE);
- } catch (IOException x) {
- JOptionPane.showMessageDialog(null, "Problemi tecnici.", "ERRORE", JOptionPane.ERROR_MESSAGE);
- } finally {
- try {
- if (i == TextArray.length) {
- lnr.close(); // chiude la risorsa
- }
- } catch (IOException x) {
- JOptionPane.showMessageDialog(null, "Problemi tecnici.", "ERRORE", JOptionPane.ERROR_MESSAGE);
- }
- }
- return dato;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment