Scanning file for particular string or character (Searching)

Last fews days ago, I saw a post on Stack Overflow in which the user wanted to search the file content. Although I didn’t answer that post because it was off-topic for that website. But, now I wanted to share the code for that particular action to the public.

I was working on this project since today morning and I finally came up with the code for that. Actually not only me, some other Helpfull fellows from the Stack Overflow community too. I had a problem in my code but the guys there helped me and made me move on to my right track. A warm thank to them.

Well the code was simple and small in size. Although it was a powerfull code. I used a simple File.txt file in the res folder and then the coding began. Here is the file content.

Ok, here is some text!

Actually this file is created to test the validity of the java application

Java is my favourite programming language. I can say that, because for my every tag

on Stack Overflow, I have no enough score. But for Java I have scored enough!

And I think I can score even more 🙂

Wish me luck!

Then I wrote the code to search this file for the character or a string the user would input. Definitely I would search for a String, not a character. Because character would always be present in the file somewhere.

The code is


import java.util.Scanner;

public class Main {
   public static void main (String[] args) throws IOException {
   // Only write the output here!!!
   System.out.print("Write the character to be found in the File: ");
   Scanner sc = new Scanner(;
   String character =;
   // Find the character
   System.out.println("Searching now...");
   // Close the resource!
 // Get character and all other methods would be here...
 public static void getCharacterLocation (String character) throws IOException {
   System.out.println("File found...");
   File file = new File("res/File.txt");
   Scanner sc = new Scanner(file);
   int lineNumber = 0;
   int totalLines = 0;
   boolean found = false;
   // First get the total number of lines
   while(sc.hasNextLine()) {
   System.out.println("Line looping! For Total Lines variable.");
   int[] lineNumbers = new int[totalLines];
   int lineIndex = 0;
   System.out.println("Searching in each line...");
   sc = new Scanner(file);
   while(sc.hasNextLine()) {
     // Until the end
     /* Get each of the character, I mean string from
     * each of the line... */
     String characterInLine = sc.nextLine().toLowerCase();
     if(characterInLine.indexOf(character.toLowerCase()) != -1) {
       found = true;
     System.out.println("Searching complete, showing results...");
     // All done! Now post that.
     if(found) {
       // Something found! :D
       System.out.print("'" + character + "' was found in the file!");
     } else {
       // Nope didn't found a fuck!
       System.out.println("Sorry, '" + character + "' didn't match any character in file  .");

Share the code, and do remember to ask for more!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s