OneCompiler

ja14

114
  1. Write a Java program for a simple search engine. Accept a string to be searched. Search
    the string in all text files in the current folder. Use a separate thread for each file. The
    result should display the filename and line number where the string is found.
    [15 M]
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;

public class SearchEngine {
public static void main(String[] args) {
String searchString = "your_search_string"; // Replace "your_search_string" with the string to be searched

    File folder = new File(".");
    File[] files = folder.listFiles();

    for (File file : files) {
        if (file.isFile() && file.getName().endsWith(".txt")) {
            Thread searchThread = new Thread(new SearchTask(file, searchString));
            searchThread.start();
        }
    }
}

}

class SearchTask implements Runnable {
private File file;
private String searchString;

public SearchTask(File file, String searchString) {
    this.file = file;
    this.searchString = searchString;
}

@Override
public void run() {
    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
        String line;
        int lineNumber = 1;
        while ((line = reader.readLine()) != null) {
            if (line.contains(searchString)) {
                System.out.println("Found '" + searchString + "' in file: " + file.getName() + " at line " + lineNumber);
            }
            lineNumber++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

  1. Write a JSP program to calculate sum of first and last digit of a given number. Display
    sum in Red Color with font size 18.
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Sum of First and Last Digit</title> </head> <body> <% int number = Integer.parseInt(request.getParameter("number")); int sum = (number % 10) + Character.getNumericValue(Integer.toString(number).charAt(0)); %> <div style="color: red; font-size: 18px;"> Sum of first and last digit of <%= number %> is <%= sum %>. </div> </body> </html>