Counting words and specific characters in a file with switch statements
<p>Alright, so basically what I have to do here is read a file containing words. Some of these words have special characters such as quotes, periods, hyphens etc at the beginning and end. I have to count all of the words in the file, count how many special characters there are, remove them, and print out each word on a seperate line without the special characters. </p>

<p>So now that we have the purpose out of the way, i'd like to explain my thought process with the coding I have below. So in the beginning I was thinking of how to use switch statements to remove specific characters, and since I think that you can only use numbers in switch statements I thought using the decimal number for a character from the ascii table would be the best way to go. So I did that, and it prints out all of the words just as they should be. However, when counting up all of the special characters it gets it completely wrong, only counting a fraction of the total amount. I am just at a loss as to why exactly it is doing this, so any help at all would be greatly appreciated!</p>

<pre><code>import java.io.File;
import java.io.IOException;
import java.util.Scanner;   

public class FileWords
{
public static void main( String [] args ) throws IOException
{
    Scanner scan = new Scanner(System.in);
    System.out.print("Please enter a file name: ");
    File file = new File(scan.next() );

    Scanner scanFile = new Scanner(file);
    String fileContent;
    int wordNum = 0, quote = 0, dubQuote = 0, semi = 0, colon = 0, period = 0, comma = 0, hyphen = 0, exclamation = 0, dollar = 0,  question = 0, words = 0;

    do
    {
        fileContent = scanFile.next();  
        wordNum = fileContent.length();     

        switch ( (fileContent.charAt(0)) )
        {
            case 34: 
            dubQuote ++;
            fileContent = fileContent.substring( 1 , wordNum + fileContent.indexOf(" ") );
            break;

            case 36: 
            dollar++;
            fileContent = fileContent.substring( 1 , wordNum + fileContent.indexOf(" ") );
            break;

            case 39:
            quote++;
            fileContent = fileContent.substring( 1 , wordNum + fileContent.indexOf(" ") );
            break;

            case 46:    
            period++;
            fileContent = fileContent.substring( 1 , wordNum + fileContent.indexOf(" ") );  
            break;

            default:
            break;  
        }               

        wordNum = fileContent.length();

        switch ( fileContent.charAt(wordNum - 1) )
        {
            case 33: 
            exclamation++;
            words++;
            fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
            break;

            case 34: 
            dubQuote ++;
            words++;
            fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
            break;

            case 39: 
            quote++;
            words++;
            fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
            break;

            case 44: 
            comma++;
            words++;
            fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
            break;

            case 45: 
            hyphen++;
            words++;
            fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
            break;

            case 46: 
            period++;
            words++;
            fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
            break;

            case 58: 
            colon++;
            words++;
            fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
            break;

            case 59: 
            semi++;
            words++;
            fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
            break;

            case 63: 
            question++;
            words++;
            fileContent = fileContent.substring( 0 , wordNum + fileContent.indexOf(" ") );
            break;

            default:
            words++;
            break;
        }


        System.out.println(fileContent);

    } // end of do
    while (scanFile.hasNext());

    System.out.println();
    System.out.println("Double Quotes:       " + dubQuote);
    System.out.println("Single Quotes:       " + quote);
    System.out.println("Semi-Colons:         " + semi);
    System.out.println("Colons:              " + colon);
    System.out.println("Periods:             " + period);
    System.out.println("Commas:              " + comma);
    System.out.println("Hyphens:             " + hyphen);
    System.out.println("Exclamation Points:  " + exclamation);
    System.out.println("Question Marks:      " + question);
    System.out.println("Dollar Signs:        " + dollar);
    System.out.println("Words Found:         " + words);

}
}
</code></pre>
