Count your lucky stars!

Okay, ignore the stars… this is a classic ‘dodgy but works’ get the job done kind of scenario where I had to modify a program last year to compare words from file X to words in file Y on a character by character basis to verify spelling – a crude spell checker, if you will.

 

Anyway, edited the code to count braces ), {, ( and }, in a given source file -> useful if your code wont compile and you want to make sure there’s the right number of brackets and braces present. Of course, strings may include such braces and throw an offset to your result but regardless, here the source and image be:-

Search.java Example

Search.java Example

//imports
import java.io.*;
import java.util.*;

//class
public class search
{
  public int n,j,k,l;
  private ArrayList<String> fc = new ArrayList<String>(); //file content

  //main 
  public static void main(String[] argz)
  {

    search s = new search(); //instance of class
	s.n=0;
	s.j=0;
	s.k=0;
	s.l=0;
    s.loadFile(); //load file content to ArrayList
    s.results();
  }

  //ask user for path to file and load contents word by word into arraylist
  public void loadFile()
  {
    System.out.println("Enter path to text file for brace counting"); //ask for file path
    Scanner scan = new Scanner(System.in); //create scanner object to obtain file path
    String filename=scan.next(); //get file path and store in string var
    File loadedFile=null; //create null File var
    try
    {
      loadedFile = new File(filename); //instantiate File obj using given file path
      scan = new Scanner(loadedFile); //instantiate new Scanner object using File object
    }
    catch(FileNotFoundException e) //handle file not found exception
    {
      //notify user that file not found and ...
      System.out.println("File '"+filename+"' not found.");
      //...exit
      System.exit(0);
    }

    String newword=""; //string var to hold each word

    while (scan.hasNextLine ()) //while next line is available ...
    {

      String line =scan.nextLine(); //...get next line from text file

      //for the length of the line, go through each character to locate next word in line
      //and store word in the newword var for adding to the file content arraylist
      for (int i=0;i<line.length();i++)
      {
	//if character does not equal , ; : ? . [whitespace] [line-break] [carriage-return]... add char to newword var...
        if (line.charAt(i) == '(')
        {
		n++;

        }
        if (line.charAt(i) == ')')
        {
		j++;

        }
        if (line.charAt(i) == '{')
        {
		k++;

        }
        if (line.charAt(i) == '}')
        {
		l++;

        }
        else //...else store word in file content arraylist and set newword var to "" aka empty string value
        {

        }
      }

    }
  }

private void results()
{
System.out.println("( found: " + n);
System.out.println(") found: " + j);
System.out.println("{ found: " + k);
System.out.println("} found: " + l);
}
}
Bookmark the permalink.

Comments are closed.