Monthly Archives: May 2014

Equivallent to Console.ReadKey() in C#

I remember the code of C++ to pause the Console to show the output. Otherwise the Console exit with either a return type returned or its data type is a void.

system("pause"); // pauses the system, Windows OS only

Well, now that I have to move to C#, I don’t find those System commands. They were a bit handy. But since they’re not built-in, they can be developed in custom. Example code would be

public void pause() {
   Console.WriteLine("Press any key to continue...");

public void main() {

Now it would execute, it would be just the way C++ paused the Console. It would show the dialoge, and then would pause the Console. The return type is void, that’s why it won’t return anything in the Stream as output either.

Pretty simple it was!

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!