Question

In: Computer Science

How to use ignore case sensitive in the following code ? /** * * * searches...

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
}
}

Solutions

Expert Solution

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
        }
}

Related Solutions

The following code searches a text file for definitions and prints them out. I want to...
The following code searches a text file for definitions and prints them out. I want to create a function that can be called for the given vector and map. The function will either print the results in reverse order, print results without duplicate definitions, or do both. What is the best approach to make the code less repetitive. if (find(keyWords.begin(), keyWords.end(), v[0]) != keyWords.end()) {                        for (auto map_iter = mymap.cbegin(); map_iter != mymap.cend(); ++map_iter)...
PLEASE USE R PROGRAMMING TO SOLVE THIS AND PASTE A COPYABLE CODE Photoresist is a light-sensitive...
PLEASE USE R PROGRAMMING TO SOLVE THIS AND PASTE A COPYABLE CODE Photoresist is a light-sensitive material applied to semiconductor wafers so that the circuit pattern can be imaged on to the wafer. After application, the coated wafers are baked to remove the solvent in the photoresist mixture and to harden the resist. Here are measurements of photoresist thickness (in kA) for eight wafers baked at two different temperatures. We want to see whether different temperatures make difference. The data...
How would I code the following in assembly language? Use the Keil programming environment to code...
How would I code the following in assembly language? Use the Keil programming environment to code the C8051F330 SiLabs 8051 micro controller. All the documentation is available on efundi. The program shall be done in assembler and you shall use the DJNZ instruction to generate a delay time to flash the LED. The LED shall flash in the following sequence: 1. On for 50mS, 2. Off for 400mS, 3. On for 50mS, 4. Off for 1.5S, 5. Repeat the sequence...
Explain how you can secure passwords and other sensitive data on a website. Provide code snippets...
Explain how you can secure passwords and other sensitive data on a website. Provide code snippets to explain your approach. Your answer can be based on the use of PHP/MySQL or ASP.NET/SQL based solution. Explain how online advertisements will be stored and served.
Case II – Godiva Case Any of irrelevant information to the question below, you can ignore...
Case II – Godiva Case Any of irrelevant information to the question below, you can ignore from the description. This case is updated or continued from the first case study in week 7. [Personal Info.] Robinson Godiva is 46 years old, and his wife Geniece is 37 years old. Robinson and Geniece were married 8 years ago; it was Robinson’s second marriage and Geniece’s first marriage. Robinson and Geniece have one child Chaplin, who is 6 years of age. Robinson...
Complete the following use case description table for the use case ‘Borrow Books’ Use Case Name...
Complete the following use case description table for the use case ‘Borrow Books’ Use Case Name Scenario Triggering Event Primary Actors Related Use Case/s Precondition Post Condition Main Flow Exceptions Special Conditions
Complete the following use case description table for the use case ‘Borrow Books’ Use Case Name...
Complete the following use case description table for the use case ‘Borrow Books’ Use Case Name Scenario Triggering Event Primary Actors Related Use Case/s Precondition Post Condition Main Flow Exceptions Special Conditions the subject is ICT103 System analysis and design of IT
Complete the following use case description table for the use case ‘Borrow Books’ Use Case Name...
Complete the following use case description table for the use case ‘Borrow Books’ Use Case Name    Scenario Triggering Event Primary Actors Related Use Case/s Precondition Post Condition Main Flow Exceptions Special Conditions
Which of the following are not sensitive to outliers?
Which of the following are not sensitive to outliers? 1. coefficient of variation 2. Mode 3. The range 4. Arithmetic mean. 5. Median 6. standard deviation 7. Z score 8. Interquartile range
Java: Make 3 use cases for the code bellow. Example of Use case: Leave a Message...
Java: Make 3 use cases for the code bellow. Example of Use case: Leave a Message 1.Caller dials main number of voice mail system 2.System speaks prompt 3.User types extension number 4.System speaks 5.Caller speaks message Variation #1 3.1 In step 3, user enters invalid extension number 3.2 Voice mail system speaks 3.3 Continue with step 2. What the code Does: adds a new regular task, delete a task , show all tasks, and show regular tasks. my code: import...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT