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:
For the Driver (Blueprint / backup):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 "); } }
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.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()); } }
All Feedback and assistance is greatly Appreciated!
Thanks