Witam. Mam do zrobienia program w javie, który wczytuje pliki txt kodów DNA, wyszukuje sekwencję znaków i porównuje je ze sobą. Czy macie jakiś pomysł, jak mogę rozwiązać ten problem za pomocą algorytmu sequential search? Poniżej wklejam kod tego, co mam.
import java.io.BufferedReader;
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 Menu {
public static String[] dnaArray = null;
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 {
dnaArray=readFile("C:\\Users\\skute\\Desktop\\JOGOS\\str\\src\\data.txt");
} 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();
System.out.println(countExists(xd,"data.txt"));
break;
case 3:
Scanner enter2 = new Scanner(System.in);
String xd2=enter2.nextLine();
sequentialSearch(dnaArray,xd2);
default:
break;
}
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].equals(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++;
}
}
}
catch (IOException e) {
e.printStackTrace();
}
return dna;
}
public static void SearchAlg1(String[] x ) {
System.out.println(x);
}
}