Per contare le linee di un file (di circa 20 righe) ho usato questo codice:
ma mi ritorna 0codice: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){ }
poi, per stampare tre parole random, ho scritto questo
e non solo non mi funzica, ma mi restituisce quest'errore su dos: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 }
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

Rispondi quotando