Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.LineNumberReader;
- public class Test {
- public static String[] dnaArray = null;
- public static long start, stop;
- public static void main(String[] args) throws FileNotFoundException {
- int option = 0;
- do {
- option = menu();
- } while (option != 0);
- System.out.println(" DNA Management application end");
- }
- private static int menu() {
- System.out.println(" DNA Management application //\r\n" +
- "// 1 ? Read file //\r\n" +
- "// 2 ? Search pattern with alg1 //\r\n" +
- "// 3 ? Search pattern with alg2 //\r\n" +
- "// 4 ? Order sequence with alg1 //\r\n" +
- "// 5 ? Order sequence with alg2 //\r\n" +
- "// 0 ? Exit //\r\n" +
- "// Option? ");
- int choose;
- Scanner odczyt = new Scanner(System.in); //obiekt do odebrania danych od u?ytkownika
- choose = odczyt.nextInt();
- //String[] c=null;
- switch (choose) {
- case 1:
- try {
- start = System.currentTimeMillis();
- dnaArray = readFile("data.txt");
- stop = System.currentTimeMillis();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(dnaArray[0]);
- break;
- case 2:
- Scanner enter = new Scanner(System.in);
- String xd = enter.nextLine();
- start = System.currentTimeMillis();
- System.out.println(countExists(xd, "data.txt"));
- stop = System.currentTimeMillis();
- break;
- case 3:
- try {
- dnaArray = readFile("data.txt");
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(dnaArray[0]);
- System.out.println("String to search?");
- Scanner enter2 = new Scanner(System.in);
- String xd2 = enter2.nextLine();
- start = System.currentTimeMillis();
- int count = sequentialSearch(dnaArray, xd2);
- stop = System.currentTimeMillis();
- System.out.println("found " + xd2 + " " + count);
- break;
- case 4:
- try {
- dnaArray = readFile("data.txt");
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- start = System.currentTimeMillis();
- Arrays.sort(dnaArray);
- stop = System.currentTimeMillis();
- for (String string : dnaArray) {
- System.out.println(string);
- }
- break;
- case 5:
- try {
- dnaArray = readFile("data.txt");
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- start = System.currentTimeMillis();
- dnaArray = sortArrayOfStrings(dnaArray);
- stop = System.currentTimeMillis();
- for (String string : dnaArray) {
- System.out.println(string);
- }
- break;
- default:
- }
- System.out.println("execute time (in millisec): "+(stop-start));
- return choose;
- }
- static int countExists(String targetString, String nazwaPliku) {
- File txt = new File(nazwaPliku);
- int count = 0;
- try {
- BufferedReader read = new BufferedReader(new FileReader(txt));
- String line;
- while ((line = read.readLine()) != null) {
- Matcher m = Pattern.compile(targetString).matcher(line);
- while (m.find()) {
- count++;
- }
- }
- read.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return count;
- }
- public static int sequentialSearch(String[] elements, String target) {
- if (elements == null)
- return -1;
- int count = 0;
- for (int j = 0; j < elements.length; j++) {
- if (elements[j] != null) {
- if (elements[j].contains(target)) {
- count++;
- }
- }
- }
- return count;
- }
- public static String[] readFile(String csvFile) throws FileNotFoundException {
- File file = new File(csvFile);
- BufferedReader in = new BufferedReader(new FileReader(file));
- LineNumberReader lnr = new LineNumberReader(new FileReader(csvFile));
- String[] dna = null;
- int count = 0;
- String temp = "";
- try {
- lnr.skip(Long.MAX_VALUE);
- dna = new String[lnr.getLineNumber() + 1];
- lnr.close();
- while ((temp = in.readLine()) != null) {
- if (temp != null) {
- dna[count] = temp;
- count++;
- }
- }
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // Arrays.sort(dna);
- return dna;
- }
- public static void SearchAlg1(String[] x) {
- System.out.println(x);
- }
- public static String[] sortArrayOfStrings(String[] array) {
- for (int i = 1; i < array.length; i++) {
- String element = array[i];
- int j;
- for (j = i - 1; j >= 0 && element.compareTo(array[j]) <= 0; j--)
- array[j + 1] = array[j];
- array[j + 1] = element;
- }
- return array;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement