JAVA 14
14 simple search engine. && JSP program to calculate sum
Q1]
import java.io.;
import java.util.;
public class SimpleSearchEngine {
private static final String SEARCH_STRING = "your_search_string";
public static void main(String[] args) {
File currentDir = new File(".");
File[] files = currentDir.listFiles();
List<Thread> threads = new ArrayList<>();
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
Thread thread = new Thread(() -> searchInFile(file));
threads.add(thread);
thread.start();
}
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static void searchInFile(File file) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
int lineNumber = 0;
while ((line = reader.readLine()) != null) {
lineNumber++;
if (line.contains(SEARCH_STRING)) {
System.out.println("String found in file: " + file.getName() + ", Line: " + lineNumber);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Q2]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
String numberStr = request.getParameter("number");
int sum = 0;
if (numberStr != null && !numberStr.isEmpty()) {
int number = Integer.parseInt(numberStr);
int lastDigit = number % 10;
while (number >= 10) {
number /= 10;
}
int firstDigit = number;
sum = firstDigit + lastDigit;
}
%>
<p style="color: red; font-size: 18px;">Sum of first and last digit of <%= numberStr %> is <%= sum %></p>
</body>
</html>