Per contare le linee di un file (di circa 20 righe) ho usato questo codice:
codice:
try{
String url = "...\\...\\parole.txt";
FileReader fr = new FileReader(url);
BufferedReader br = new BufferedReader(fr);
LineNumberReader lnr = new LineNumberReader(br);
totale = lnr.getLineNumber();
System.out.println("Il totale delle parole e' : " + totale);
} catch(FileNotFoundException fnfe){ }
ma mi ritorna 0
poi, per stampare tre parole random, ho scritto questo
codice:
//nw è il numero di parole da scrivere
Random casuale;
for(int k=1;k<nw;k++){
casuale = new Random();
casuale2 = casuale.nextInt(totale);
System.out.println("La parola trovata e' di numero " + casuale2);
// o anche + casuale
}
e non solo non mi funzica, ma mi restituisce quest'errore su dos:
java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Random.java:251)
at sparola.paint(sparola.java:66)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at sun.awt.windows.WComponentPeer.handleEvent(WCompon entPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.jav a:3678)
at java.awt.Container.dispatchEventImpl(Container.jav a:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606 )
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 456)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:100)
Ciao ciao