mercredi 31 août 2016

Import csv perform searching without the use of opencsv

I want to import csv file without using opencsv and store those data in collection. From that collection draw any random data and show it on the screen. (Using java pre-defined packages only. Not opencsv)

package panellist;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Random;
import java.util.Set;
public class PanelList {
private static Random r = new Random();
private static int rand;
private static int endRange = 20;
private static List<Integer> randomNumber;
public static void main(String[] args) {

    String fileName = "C:\\Users\\Ankit\\Desktop\\Book2.csv";
File file = new File(fileName); // TODO: read about File Names
Scanner scan = new Scanner(System.in);
HashMap map = new HashMap();
ArrayList al = new ArrayList();
int i =0;



try {
    Scanner inputStream = new Scanner(file);
    while (inputStream.hasNext()){
        String data = inputStream.next();
        System.out.println("test="+data/*.startsWith("a")*/);
        map.put(i++,data);
        StringTokenizer st = new StringTokenizer(data,",");  
 while (st.hasMoreTokens()) {  
     System.out.println(st.nextToken());  
 }
    }
    inputStream.close();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println("Printing HashMap:");
System.out.println(map);

endRange = map.size()-1;
for(int j=0;j<endRange;j++){
    rand = r.nextInt(endRange);
    System.out.println(map.get(r));
}



System.out.println(endRange);
rand = r.nextInt(endRange);           
       do
        {
           rand = r.nextInt(endRange);
        }
        while(randomNumber.contains(rand));

        randomNumber.add(rand);    
            Object x= randomNumber.get(randomNumber.size()-1);
       System.out.println(x);


}

}




Aucun commentaire:

Enregistrer un commentaire