Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: A program that counts the number of punctuation marks in a text file

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A program that counts the number of punctuation marks in a text file

    So I've attempted it, and in the end, i ended up getting the same values for most of my punctuations, the wrong value, so i want to know
    where I could be going wrong:

    import java.io.FileNotFoundException;


    public class FileDriver
    {

    public static void main(String[] args) throws FileNotFoundException
    {
    OperationsForFile fileActions = new OperationsForFile();
    fileActions.readFileIntoList("frankenstein.txt");

    int sentences = fileActions.countSentences();
    System.out.println("Frankenstein consists of " + sentences + " sentences ");

    int QuestionMarks = fileActions.countQuestionMarks();
    System.out.println("Frankenstein consists of " + QuestionMarks
    + " Question Marks ");

    int ExclaimationMarks = fileActions.countExclaimationMarks();
    System.out.println("Frankenstein consists of " + ExclaimationMarks
    + " Exclaimation Marks ");

    int Colon = fileActions.countColon();
    System.out.println("Frankenstein consists of " + Colon + " Colons ");

    int SemiColon = fileActions.countSemiColon();
    System.out
    .println("Frankenstein consists of " + SemiColon + " Semi-Colons");

    int Dash = fileActions.countDash();
    System.out.println("Frankenstein consists of " + Dash + " Dashes ");

    int UnderScore = fileActions.countUnderScore();
    System.out.println("Frankenstein consists of " + UnderScore
    + " Underscores ");

    int Brackets = fileActions.countBrackets();
    System.out.println("Frankenstein consists of " + Brackets + " Brackets ");

    int Apostrophe = fileActions.countApostrophe();
    System.out.println("Frankenstein consists of " + Apostrophe + " 's");

    int Quotation = fileActions.countQuotation();
    System.out
    .println("Frankenstein consists of " + Quotation + " Quotations ");

    int Slash = fileActions.countSlash();
    System.out.println("Frankenstein consists of " + Slash + " Slashes ");

    int Comma = fileActions.countComma();
    System.out.println("Frankenstein consists of " + Comma + " Commas ");


    }

    }

    And then the driver behind this class:

    public class OperationsForFile
    {
    private ArrayList<String> lines = new ArrayList<String>();

    public int countSentences()
    {
    int lineNumber = 0;
    int numberOfPeriods = 0;
    while (lineNumber < this.lines.size())
    {
    String line = this.lines.get(lineNumber);
    int charCount = 0;
    while (charCount < line.length())
    {
    char myChar = line.charAt(charCount);
    if (myChar == '.')
    {
    // update each step, make sure everything are separate variables
    numberOfPeriods++;
    }
    charCount++;
    }
    lineNumber++;
    }
    return numberOfPeriods;
    }


    public int countQuestionMarks()
    {
    int lineNumber1 = 0;
    int numberOfQuestionMarks = 0;
    while (lineNumber1 < this.lines.size())
    {
    String line1 = this.lines.get(lineNumber1);
    int charCount1 = 0;
    while (charCount1 < line1.length())
    {
    char myChar1 = line1.charAt(charCount1);
    if (myChar1 == '?')
    ;
    {
    numberOfQuestionMarks++;
    }
    charCount1++;

    }
    lineNumber1++;
    }
    return numberOfQuestionMarks;
    }


    public int countExclaimationMarks()
    {
    int lineNumber2 = 0;
    int numberOfExclaimationMarks = 0;
    while (lineNumber2 < this.lines.size())
    {
    String line2 = this.lines.get(lineNumber2);
    int charCount2 = 0;
    while (charCount2 < line2.length())
    {
    char myChar2 = line2.charAt(charCount2);
    if (myChar2 == '!')
    ;
    {
    numberOfExclaimationMarks++;
    }
    charCount2++;
    }
    lineNumber2++;
    }
    return numberOfExclaimationMarks;
    }


    public int countColon()
    {
    int lineNumber3 = 0;
    int numberOfColons = 0;
    while (lineNumber3 < this.lines.size())
    {
    String line3 = this.lines.get(lineNumber3);
    int charCount3 = 0;
    while (charCount3 < line3.length())
    {
    char myChar3 = line3.charAt(charCount3);
    if (myChar3 == ':')
    ;
    {
    numberOfColons++;
    }
    charCount3++;
    }
    lineNumber3++;
    }
    return numberOfColons;
    }


    public int countSemiColon()
    {
    int lineNumber4 = 0;
    int numberOfSemiColon = 0;
    while (lineNumber4 < this.lines.size())
    {
    String line4 = this.lines.get(lineNumber4);
    int charCount4 = 0;
    while (charCount4 < line4.length())
    {
    char myChar4 = line4.charAt(charCount4);
    if (myChar4 == ';')
    ;
    {
    numberOfSemiColon++;
    }
    charCount4++;
    }
    lineNumber4++;
    }
    return numberOfSemiColon;
    }


    public int countDash()
    {
    int lineNumber5 = 0;
    int numberOfDash = 0;
    while (lineNumber5 < this.lines.size())
    {
    String line5 = this.lines.get(lineNumber5);
    int charCount5 = 0;
    while (charCount5 < line5.length())
    {
    char myChar5 = line5.charAt(charCount5);
    if (myChar5 == '-')
    ;
    {
    numberOfDash++;
    }
    charCount5++;
    }
    lineNumber5++;
    }
    return numberOfDash;
    }


    public int countUnderScore()
    {
    int lineNumber6 = 0;
    int numberOfUnderScore = 0;
    while (lineNumber6 < this.lines.size())
    {
    String line6 = this.lines.get(lineNumber6);
    int charCount6 = 0;
    while (charCount6 < line6.length())
    {
    char myChar6 = line6.charAt(charCount6);
    if (myChar6 == '_')
    ;
    {
    numberOfUnderScore++;
    }
    charCount6++;
    }
    lineNumber6++;
    }
    return numberOfUnderScore;
    }


    public int countBrackets()
    {
    int lineNumber7 = 0;
    int numberOfLeftBrackets = 0;
    while (lineNumber7 < this.lines.size())
    {
    String line7 = this.lines.get(lineNumber7);
    int charCount7 = 0;
    while (charCount7 < line7.length())
    {
    char myChar7 = line7.charAt(charCount7);
    if (myChar7 == '(')
    ;
    {
    numberOfLeftBrackets++;
    }
    lineNumber7++;
    }
    charCount7++;
    {

    }
    }
    return numberOfLeftBrackets;
    }


    public int countRightBrackets()
    {
    int lineNumber8 = 0;
    int numberOfRightBrackets = 0;
    while (lineNumber8 < this.lines.size())
    {
    String line8 = this.lines.get(lineNumber8);
    int charCount8 = 0;
    while (charCount8 < line8.length())
    {
    char myChar8 = line8.charAt(charCount8);
    if (myChar8 == ')')
    ;
    {
    numberOfRightBrackets++;
    }
    lineNumber8++;
    }
    charCount8++;
    {

    }
    }
    return numberOfRightBrackets;
    }


    public int countApostrophe()
    {
    int lineNumber9 = 0;
    int numberOfApostrophe = 0;
    while (lineNumber9 < this.lines.size())
    {
    String line9 = this.lines.get(lineNumber9);
    int charCount9 = 0;
    while (charCount9 < line9.length())
    {
    char myChar9 = line9.charAt(charCount9);
    if (myChar9 == ';')
    ;
    {
    numberOfApostrophe++;
    }
    lineNumber9++;
    }
    charCount9++;
    {

    }
    }
    return numberOfApostrophe;
    }


    public int countQuotation()
    {
    int lineNumber10 = 0;
    int numberOfQuotation = 0;
    while (lineNumber10 < this.lines.size())
    {
    String line10 = this.lines.get(lineNumber10);
    int charCount10 = 0;
    while (charCount10 < line10.length())
    {
    char myChar10 = line10.charAt(charCount10);
    if (myChar10 == '"')
    ;
    {
    numberOfQuotation++;
    }
    lineNumber10++;
    }
    charCount10++;
    {

    }
    }
    return numberOfQuotation;
    }


    public int countSlash()
    {
    int lineNumber11 = 0;
    int numberOfSlash = 0;
    while (lineNumber11 < this.lines.size())
    {
    String line11 = this.lines.get(lineNumber11);
    int charCount11 = 0;
    while (charCount11 < line11.length())
    {
    char myChar11 = line11.charAt(charCount11);
    if (myChar11 == '/')
    ;
    {
    numberOfSlash++;
    }
    lineNumber11++;
    }
    charCount11++;
    {

    }
    }
    return numberOfSlash;
    }


    public int countComma()
    {
    int lineNumber12 = 0;
    int numberOfComma = 0;
    while (lineNumber12 < this.lines.size())
    {
    String line12 = this.lines.get(lineNumber12);
    int charCount12 = 0;
    while (charCount12 < line12.length())
    {
    char myChar12 = line12.charAt(charCount12);
    if (myChar12 == ',')
    ;
    {
    numberOfComma++;
    }
    lineNumber12++;
    }
    charCount12++;
    {

    }
    }
    return numberOfComma;
    }


    public void readFileIntoList(String fileName) throws FileNotFoundException
    {
    Scanner fileScan = new Scanner(new File(fileName));

    while (fileScan.hasNext())
    {
    lines.add(fileScan.nextLine());

    System.out.println(lines);
    }

    System.out.println("Number of Lines :" + lines.size());
    }
    }

    I've formatted to what my teacher likes so hopefully this time its easier to read

    Thanks!


  2. #2
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: A program that counts the number of punctuation marks in a text file

    Quote Originally Posted by Twoacross View Post
    So I've attempted it, and in the end, i ended up getting the same values for most of my punctuations, the wrong value, so i want to know
    where I could be going wrong:

    I've formatted to what my teacher likes so hopefully this time its easier to read

    Thanks!
    first its not easy to read in here please use the [ highlight = Java ] code here end [\highlight] with out spaces near equals

    secondly all i have done was add a few System.out.println() statements and I think you will find you mistakes very fast.

    take a look here:
    class OperationsForFile {
     
        private ArrayList<String> lines = new ArrayList<String>();
     
        public int countSentences(String s) {
     
    //moved up for scope
            int lineNumber = 0;
            int numberOfPeriods = 0;
            int charCount = 0;
     
            while (lineNumber < lines.size()) {
     
                String line = this.lines.get(lineNumber);
     
                while (charCount < line.length()) {
                    char myChar = line.charAt(charCount);
                    if (myChar == '.') {
    // update each step, make sure everything are separate variables
                        numberOfPeriods++;
                    }
                    charCount++;
     
                }
                System.out.println("char " +charCount); //should have multiple outputs here 
                charCount = 0;//reset value for next line
                lineNumber++;
     
            }
            System.out.println("lines " +lineNumber);// this is correct
     
            System.out.println("periods " +numberOfPeriods);// got the right number here as well within the entire doc
     
            return numberOfPeriods; //what is going on here??? you have three values...... review the scope of variables here.
        }//end method countSentences(String s) <----- return is not valid

    my results

    the array printed
    [hasdlfkja;sldfj,,asd;lf'ks;d,;asdf;sd.asd;asdf sd;lfasd]
    [hasdlfkja;sldfj,,asd;lf'ks;d,;asdf;sd.asd;asdf sd;lfasd, sadl;kfjas;df/dfa;lsf]
    [hasdlfkja;sldfj,,asd;lf'ks;d,;asdf;sd.asd;asdf sd;lfasd, sadl;kfjas;df/dfa;lsf, ;lasdkfj]
    [hasdlfkja;sldfj,,asd;lf'ks;d,;asdf;sd.asd;asdf sd;lfasd, sadl;kfjas;df/dfa;lsf, ;lasdkfj, ;lsakfdj;lsadj;l]
    [hasdlfkja;sldfj,,asd;lf'ks;d,;asdf;sd.asd;asdf sd;lfasd, sadl;kfjas;df/dfa;lsf, ;lasdkfj, ;lsakfdj;lsadj;l, ;sadfj;asldf.]

    Number of Lines :5
    char 55
    char 21
    char 8
    char 16
    char 13
    lines 5
    periods 2

    use some output statements you'll be fine.
    Last edited by william; October 21st, 2011 at 04:08 PM.

Similar Threads

  1. How do I make this Palindrome tester ignore punctuation?
    By trogan234 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2011, 04:43 AM
  2. Text file to text area and Radiobuttons?
    By donaldmax in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 27th, 2011, 04:45 AM
  3. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  4. Program that reads data from a text file...need help
    By cs91 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 3rd, 2010, 07:57 AM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM