In: Computer Science
How to use ignore case sensitive in the following code ?
/**
*
*
* searches for a specific file and prints the location (path)
* of any files with a matching name
*/
import java.io.*;
import java.util.Scanner;
public class FileSearch
{
public static void main(String[] args)
{
FileCrawler crawler = new FileCrawler();
Scanner input = new Scanner(System.in);
System.out.println("Please enter a folder to search:");
File directory = new File("./" + input.nextLine());
System.out.println("Please enter a file to search for:");
String filename = input.nextLine();
search(filename, crawler, directory);
}
public static void search(String filename,
FileCrawler crawler, File directory)
{
// 1. Enter StartingDirectory
crawler.enterSubdirectory(directory);
// 2. (search Files, search More Files, ...) <--
summarize
// a- list of all subdirectory from crawler
File[] get_sub= crawler.getSubdirectories();
// b- loop for the list
for (File folder : get_sub) {
// c- inside the loop call search (for subdirectory)
search( filename , crawler, folder);
}
// 3. (check file1, check file3, ...) <-- summarize
File[] get_file= crawler.getFiles();
for (File file : get_file) {
if (file.getName().equals(filename))
{
System.out.println(file.getPath());
}
}
// 4. Exit StartingDirectory
crawler.backtrack();
// TODO: implement your recursive algorithm here
}
}
To compare two strings and check if both are equal we generally use two methods:
1. equals(String str)
2. equalsIgnoreCase(String str)
Both works in same way. the only difference is that equals() checks with case sensitivity and equalsIgnoreCase ignores the case insenstivity.
So we just need to use equalsIgnoreCase in place of equals.
So we need to just replace below code
for (File file : get_file) {
if
(file.getName().equals(filename)) {
System.out.println(file.getPath());
}
}
to
for (File file : get_file) {
if
(file.getName().equalsIgnoreCase(filename)) {
System.out.println(file.getPath());
}
}
Below is your complete code.
/**
*
*
* searches for a specific file and prints the location (path)
* of any files with a matching name
*/
import java.io.*;
import java.util.Scanner;
public class FileSearch {
public static void main(String[] args) {
FileCrawler crawler = new FileCrawler();
Scanner input = new Scanner(System.in);
System.out.println("Please enter a folder to search:");
File directory = new File("./" + input.nextLine());
System.out.println("Please enter a file to search for:");
String filename = input.nextLine();
search(filename, crawler, directory);
}
public static void search(String filename, FileCrawler crawler,
File directory) {
// 1. Enter StartingDirectory
crawler.enterSubdirectory(directory);
// 2. (search Files, search More Files, ...) <-- summarize
// a- list of all subdirectory from crawler
File[] get_sub = crawler.getSubdirectories();
// b- loop for the list
for (File folder : get_sub) {
// c- inside the loop call search (for subdirectory)
search(filename, crawler, folder);
}
// 3. (check file1, check file3, ...) <-- summarize
File[] get_file = crawler.getFiles();
for (File file : get_file) {
if (file.getName().equals(filename)) {
System.out.println(file.getPath());
}
}
// 4. Exit StartingDirectory
crawler.backtrack();
// TODO: implement your recursive algorithm here
}
}