-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountSentences.java
More file actions
47 lines (36 loc) · 1.36 KB
/
CountSentences.java
File metadata and controls
47 lines (36 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Scanner;
import java.io.File;
/**
* ICE 10/23/2018 - Exercise 1
* Finds the number of sentences in Frakenstein by counting the number
* of periods.
*
* @author Daniel Law
* @version 10/23/2018
*/
public class CountSentences
{
public static void main(String[] args)
{
try {
File book = new File ("Mary Shelly Frankenstein.txt");
Scanner fin = new Scanner(book);
int numSentences = 0;
while ( fin.hasNext() )
{
String token = fin.next();
// -- Your code BEGINS here
//Find the last character by using the last index
int lastIndex = token.length()-1;
String lastChar = token.substring( lastIndex );
//Check if the last character is a period
if ( lastChar.equals(".") )
numSentences++;
if ( token.substring( token.length()-1 ).equals(".") )
numSentences++;
// -- Your code ENDS here
}
System.out.println("Number of sentences: " + numSentences);
} catch (Exception e) {System.out.println(e);}
}
}