Skip to main content

Punctuation Counter from file JAva Please HELP

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
o3iu06
Offline
Joined: 2013-07-10

Programming project: Design and implement a program that counts the number of punctuation marks in a text input file. Produce a table that shows how many times each symbol occurred. Use the following list of punctuation marks: , : ; . ! - ' " ? / \

Please help. I am trying to count the number of each character but I get this error: Exception in thread "main" java.lang.NullPointerException
at Count.(Count.java:19)
at ReadFile.main(ReadFile.java:13)

I dont know whats wrong... Thanks

import java.util.Scanner;
import java.io.*;

public class ReadFile {

public static void main(String[] args) throws IOException {
Scanner fileScan;

fileScan = new Scanner(new File("parsing2.txt"));

// Read and process each line of the file

Count count = new Count(fileScan);
System.out.println(count);
}

}
import java.util.Scanner;

public class Count {

public Scanner fileScan;
public int numColon = 0;
public int numPeriod = 0;
public int numSemi = 0;
public int numExclam = 0;
public int numQuest = 0;
public int numDash = 0;
public int numQuote = 0;
public int numApost = 0;
public int numForward = 0;
public int numBackward = 0;

public Count(Scanner scan) {
while (scan.hasNext()) {
String file = fileScan.nextLine();

for (int position = 0; position < file.length(); position++) {
if (file.charAt(position) == ':')
numColon++;
if (file.charAt(position) == '.')
numPeriod++;
if (file.charAt(position) == ';')
numSemi++;
if (file.charAt(position) == '!')
numExclam++;
if (file.charAt(position) == '?')
numQuest++;
if (file.charAt(position) == '-')
numDash++;
if (file.charAt(position) == '"')
numQuote++;
if (file.charAt(position) == '\'')
numApost++;
if (file.charAt(position) == '/')
numForward++;
if (file.charAt(position) == '\\')
numBackward++;
}
}
}

public String toString() {
String s = ("Punctuation Count: \nColon - " + numColon + "\nPeriod - "
+ numPeriod + "\nSemiColon - " + numSemi
+ "\nExclamation Point - " + numExclam + "\nQuestion Mark - "
+ numQuest + "\nDash - " + numDash + "\nQuote - " + numQuote
+ "\nApostrophe - " + numApost + "\nForward Slash - "
+ numForward + "\nBack Slash - " + numBackward + "\n");
return s;
}

}