Advertisement
maxis27

Untitled

Dec 11th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.46 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.Scanner;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileReader;
  10. import java.io.IOException;
  11. import java.io.LineNumberReader;
  12.  
  13. public class Test {
  14.  
  15.     public static String[] dnaArray = null;
  16.     public static long start, stop;
  17.  
  18.     public static void main(String[] args) throws FileNotFoundException {
  19.  
  20.         int option = 0;
  21.  
  22.         do {
  23.             option = menu();
  24.         } while (option != 0);
  25.         System.out.println(" DNA Management application end");
  26.     }
  27.  
  28.     private static int menu() {
  29.         System.out.println(" DNA Management application //\r\n" +
  30.                 "// 1 ? Read file //\r\n" +
  31.                 "// 2 ? Search pattern with alg1 //\r\n" +
  32.                 "// 3 ? Search pattern with alg2 //\r\n" +
  33.                 "// 4 ? Order sequence with alg1 //\r\n" +
  34.                 "// 5 ? Order sequence with alg2 //\r\n" +
  35.                 "// 0 ? Exit //\r\n" +
  36.                 "// Option? ");
  37.         int choose;
  38.         Scanner odczyt = new Scanner(System.in); //obiekt do odebrania danych od u?ytkownika
  39.  
  40.         choose = odczyt.nextInt();
  41.  
  42.         //String[] c=null;
  43.  
  44.         switch (choose) {
  45.             case 1:
  46.                 try {
  47.                     start = System.currentTimeMillis();
  48.                     dnaArray = readFile("data.txt");
  49.                     stop = System.currentTimeMillis();
  50.  
  51.                 } catch (FileNotFoundException e) {
  52.                     // TODO Auto-generated catch block
  53.                     e.printStackTrace();
  54.                 }
  55.                 System.out.println(dnaArray[0]);
  56.                 break;
  57.             case 2:
  58.                 Scanner enter = new Scanner(System.in);
  59.                 String xd = enter.nextLine();
  60.                
  61.                 start = System.currentTimeMillis();
  62.                 System.out.println(countExists(xd, "data.txt"));
  63.                 stop = System.currentTimeMillis();
  64.                 break;
  65.             case 3:
  66.                 try {
  67.                     dnaArray = readFile("data.txt");
  68.  
  69.                 } catch (FileNotFoundException e) {
  70.                     // TODO Auto-generated catch block
  71.                     e.printStackTrace();
  72.                 }
  73.  
  74.  
  75.                 System.out.println(dnaArray[0]);
  76.                 System.out.println("String to search?");
  77.                 Scanner enter2 = new Scanner(System.in);
  78.                 String xd2 = enter2.nextLine();
  79.  
  80.                 start = System.currentTimeMillis();
  81.                 int count = sequentialSearch(dnaArray, xd2);
  82.                 stop = System.currentTimeMillis();
  83.                 System.out.println("found " + xd2 + " " + count);
  84.                 break;
  85.             case 4:
  86.                 try {
  87.                     dnaArray = readFile("data.txt");
  88.  
  89.                 } catch (FileNotFoundException e) {
  90.                     // TODO Auto-generated catch block
  91.                     e.printStackTrace();
  92.                 }
  93.                
  94.                 start = System.currentTimeMillis();
  95.                 Arrays.sort(dnaArray);
  96.                 stop = System.currentTimeMillis();
  97.                
  98.                 for (String string : dnaArray) {
  99.                     System.out.println(string);
  100.                 }
  101.                
  102.                 break;
  103.             case 5:
  104.                 try {
  105.                     dnaArray = readFile("data.txt");
  106.  
  107.                 } catch (FileNotFoundException e) {
  108.                     // TODO Auto-generated catch block
  109.                     e.printStackTrace();
  110.                 }
  111.                
  112.                 start = System.currentTimeMillis();
  113.                 dnaArray = sortArrayOfStrings(dnaArray);
  114.                 stop = System.currentTimeMillis();
  115.                
  116.                 for (String string : dnaArray) {
  117.                     System.out.println(string);
  118.                 }
  119.                
  120.                 break;
  121.             default:
  122.         }
  123.        
  124.         System.out.println("execute time (in millisec): "+(stop-start));
  125.  
  126.         return choose;
  127.     }
  128.  
  129.     static int countExists(String targetString, String nazwaPliku) {
  130.         File txt = new File(nazwaPliku);
  131.         int count = 0;
  132.         try {
  133.             BufferedReader read = new BufferedReader(new FileReader(txt));
  134.             String line;
  135.             while ((line = read.readLine()) != null) {
  136.                 Matcher m = Pattern.compile(targetString).matcher(line);
  137.                 while (m.find()) {
  138.                     count++;
  139.                 }
  140.             }
  141.             read.close();
  142.         } catch (IOException e) {
  143.             e.printStackTrace();
  144.         }
  145.         return count;
  146.     }
  147.  
  148.     public static int sequentialSearch(String[] elements, String target) {
  149.  
  150.  
  151.         if (elements == null)
  152.             return -1;
  153.  
  154.         int count = 0;
  155.         for (int j = 0; j < elements.length; j++) {
  156.             if (elements[j] != null) {
  157.                 if (elements[j].contains(target)) {
  158.                     count++;
  159.                 }
  160.             }
  161.         }
  162.         return count;
  163.     }
  164.  
  165.     public static String[] readFile(String csvFile) throws FileNotFoundException {
  166.         File file = new File(csvFile);
  167.         BufferedReader in = new BufferedReader(new FileReader(file));
  168.         LineNumberReader lnr = new LineNumberReader(new FileReader(csvFile));
  169.         String[] dna = null;
  170.  
  171.         int count = 0;
  172.         String temp = "";
  173.  
  174.         try {
  175.             lnr.skip(Long.MAX_VALUE);
  176.             dna = new String[lnr.getLineNumber() + 1];
  177.             lnr.close();
  178.  
  179.  
  180.             while ((temp = in.readLine()) != null) {
  181.  
  182.                 if (temp != null) {
  183.  
  184.  
  185.                     dna[count] = temp;
  186.                     count++;
  187.                 }
  188.             }
  189.  
  190.             in.close();
  191.  
  192.         } catch (IOException e) {
  193.  
  194.             e.printStackTrace();
  195.         }
  196.        // Arrays.sort(dna);
  197.         return dna;
  198.  
  199.     }
  200.  
  201.     public static void SearchAlg1(String[] x) {
  202.  
  203.  
  204.         System.out.println(x);
  205.  
  206.  
  207.     }
  208.    
  209.     public static String[] sortArrayOfStrings(String[] array) {
  210.         for (int i = 1; i < array.length; i++) {
  211.             String element = array[i];
  212.             int j;
  213.             for (j = i - 1; j >= 0 && element.compareTo(array[j]) <= 0; j--)
  214.                 array[j + 1] = array[j];
  215.  
  216.             array[j + 1] = element;
  217.         }
  218.         return array;
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement