There are two txt files. In the first one there is a list of the necessary lines, in the second one there is a large accumulation of random nonsense from which you need to take the lines starting the same as the lines in the first file.
Contents of the first (list):
test test1 test2 Contents of the second (all):
test test1 .02 test4 .03 Actually, I just can’t implement a method for solving the task, displays only the very first line, which is no different. Please help and advice. The result should output this:
test test1 .02 ///строчку test4 .03 не выведет, так как её нет в содержимом первого файла. I enclose my code:
ArrayList<String> fin = new ArrayList<>(); ArrayList<String> fin2 = new ArrayList<>(); Scanner sc1 = new Scanner(new FileReader("C:\\1\\list.txt")); Scanner sc2 = new Scanner(new FileReader("C:\\1\\all.txt")); while (sc1.hasNext()){ String str = sc1.nextLine(); fin.add(str); while(sc2.hasNext()){ String str2 = sc2.nextLine(); if(str2.startsWith(fin.get(fin.size()-1))){ fin2.add(str2); } } }
нужно взять строки, начинающиеся так же, then the linetest4 .03will be selected, since it starts withtest. - post_zeew