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: Displaying the other

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

    Default Displaying the other

    Im doing a project, taking a regular text file, and then scan for how much a certain punctuation type appears in a text file. Im trying to perform this action with 2 classes instead of one, but 1 does seem much simplier to use.

    So my code:
    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 ");
     
     
     
     
     
     
      }
     
    }
    For the Driver (Blueprint / backup):
    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);
          while (inLine.hasNext);
     
          {
            words = inLine.next();
            System.out.println(lines);
          }
        }
     
        System.out.println("Number of Lines :" + lines.size());
      }
    }
    When i go through with this project, the number of sentences is the number im looking for but for the others, i cant see see why they couldbe exactly approved.

    All Feedback and assistance is greatly Appreciated!

    Thanks
    Last edited by helloworld922; October 21st, 2011 at 09:02 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Displaying the other

    I am not sure what you are asking, and you've posted tons of unformatted code that many will have a hard time going through. I recommend reading the getting help link in my signature.

Similar Threads

  1. Displaying increments of 0.5
    By PsYNus in forum Loops & Control Statements
    Replies: 3
    Last Post: September 27th, 2011, 11:27 AM
  2. Not displaying
    By toksiks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:32 AM
  3. Everything but JLabel is displaying
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 3rd, 2011, 10:30 PM
  4. Not Displaying what I need
    By rob3097 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2010, 10:31 PM
  5. Displaying things in gui
    By KrisTheSavage in forum AWT / Java Swing
    Replies: 2
    Last Post: March 29th, 2010, 12:21 PM

Tags for this Thread