full documentation here; http://cs.boisestate.edu/~cs121/projects/p4/ Here is my
ID: 3731802 • Letter: F
Question
full documentation here; http://cs.boisestate.edu/~cs121/projects/p4/
Here is my code currently, I need to implement an array that contains all of the letters of the alphabet and also counts them throughout the program. Also I'm not sure where Im supposed to implement my charCounter which counts the total number of char in the whole file. Also, the charCount is supposed to count all whitespace, letters and punctuation etc., and how do i implement that?
Here it what I currently have
// Instance Variables
private int charCount;
private int wordCount;
private int lineCount;
private int[] letterCount;
private int[] wordLengthCount;
private double avgWordLength;
private File file;
private static final String DELIMITERS = "[\W\d_]+";
private final int ERROR_CODE = 1;
private Scanner scan;
private Scanner fileScan;
private Scanner lineScan;
private String filename;
// Constructors
public TextStatistics(File file)
{
this.file = file;
scan = new Scanner(System.in);
file = new File(filename);
letterCount = new int[26];
if(file.exists() && file.isFile())
{
try
{
charCount = 0;
wordCount = 0;
lineCount = 0;
letterCount = new int[26];
fileScan = new Scanner(file);
while(fileScan.hasNextLine())
{
String line = scan.nextLine();
lineCount ++;
lineScan = new Scanner(line);
lineScan.useDelimiter(DELIMITERS);
while(lineScan.hasNext())
{
String word = lineScan.next();
wordCount ++;
}
}
}
catch(FileNotFoundException e)
{
System.out.println("File "" + filename + "" could not be opened.");
System.out.println(e.getMessage());
System.exit(ERROR_CODE);
}
}
else
{
System.out.println("Sorry, file: " + filename + " does not exist.");
}
}
Explanation / Answer
public TextStatistics(File file)
{
this.file = file;
scan = new Scanner(System.in);
file = new File(filename);
letterCount = new int[26];
if(file.exists() && file.isFile())
{
try
{
charCount = 0;
wordCount = 0;
lineCount = 0;
letterCount = new int[26];
fileScan = new Scanner(file);
while(fileScan.hasNextLine())
{
String line = scan.nextLine();
for(int i=0;i<line.length();i++)
if(line.charAt(i)!='[' && line.charAt(i)!=']' && line.charAt(i)!='/' && line.charAt(i)!='_' && line.charAt(i)!='+')
charCount += 1;/*here you have to increment charCount for every character otherthan the delimiters [/_+] excluding other two which comes under letters*/
lineCount ++;
lineScan = new Scanner(line);
lineScan.useDelimiter(DELIMITERS);
while(lineScan.hasNext())
{
String word = lineScan.next();
wordCount ++;
}
}
}
catch(FileNotFoundException e)
{
System.out.println("File "" + filename + "" could not be opened.");
System.out.println(e.getMessage());
System.exit(ERROR_CODE);
}
}
else
{
System.out.println("Sorry, file: " + filename + " does not exist.");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.