this code runs succesfully onnet bean, though it compiled in prompt but
compiler is telling me that it is not checked

import java.io.*;         
import java.util.*;
class RandomSelection {




        
    public static void main(String[] args) {    

try{
        FileInputStream  fstream = new FileInputStream("c:\\filea.txt");
    	
         DataInputStream in = new DataInputStream(fstream);
        BufferedReader  br = new BufferedReader(new InputStreamReader(in));
  	LineNumberReader  ln = new LineNumberReader(br);

      	int count = 0;

    	while (ln.readLine() != null){

    		count++;
	}
	ln.close();
	in.close();
     

      
	fstream = new FileInputStream("c:\\filea.txt");

    	in = new DataInputStream(fstream);
        br = new BufferedReader(new InputStreamReader(in));
    	String strLine;
  	ln = new LineNumberReader(br);
        Random mixture = new Random();
        HashSet usedNums = new HashSet();

        int numberIndex;
		String[] see = new String[count];
 		int i = 0;

	  while ((strLine = br.readLine()) != null)   {



			see[i] = strLine;

	              i = i + 1;
          }
                           for ( i=1; i <=200; i++) {
                           do{numberIndex = mixture.nextInt(see.length);
                           }

                           while(!usedNums.add(numberIndex) );


                     System.out.println(see[numberIndex]);
                          }


      ln.close();
       in.close();

 }
    catch (Exception e){
      System.err.println("Error: " + e.getMessage());
    }
  }
}

Recommended Answers

All 4 Replies

Change line 36
from: HashSet usedNums = new HashSet(); to: HashSet[B]<Integer>[/B] usedNums = new HashSet[B]<Integer>[/B]();

commented: this guy is too good +0

This loop loops infinitely.

do
{
  numberIndex = mixture.nextInt(see.length);
}

while(!usedNums.add(numberIndex) );

Check the javaDoc for HashSet and look to the add method.

Hope it helps.

Hapy to helps good lock

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.