I can\'t get my program to calculte the totals for the vowels and numbers. If I
ID: 3741129 • Letter: I
Question
I can't get my program to calculte the totals for the vowels and numbers. If I take those out, the punctuation works fine, but when I add them back in, it doesn't work, so something is missing to link them all together. I can't use arrays, booleans, or separate classes (like public/private).
The code:
//1. Design and implement a program that counts the number of punctuation marks, vowels,
// and numbers in a text input file.
//2. Create a counter variable for each value that you are counting. (A minimum of 9)
//3. Your program must use if statement(s). No switch statements are allowed.
//4. Your program will keep track of the following punctuation:
//.......a. ( left parenthesis
//.......b. ) right parenthesis
//.......c. , commas
//.......d. . periods
//.......e. ! exclamation points
//.......f. ? question marks
//.......g. * asterisk marks
//5. Your program will also keep track of the total number of vowels (a,e,i,o,u) in the file.
// I only want one total for this. I do not want a total for each vowel.
// Make sure you consider both capital and lowercase vowels in your count.
// Your program should be checking all variation of vowels, even if some don’t occur in the supplied text.
// (For example, there might not be a capital U in the supplied file but your program still needs to check for it.)
//6. Your program will also keep track of numeric digits in the file. I only want one total for this.
// Realize that the number 2900 would contain 4 numeric digits
//7. Results should be placed in the standard input/output window.
// Display totals for each of the 7 punctuations, 1 vowel total and 1 digit total.
import java.io.File;
import java.util.Scanner;
import java.io.*;
public class TestB
{
public static void main(String[] args) throws IOException
{
String ch;
int punctuations=0;
//variables
int left_paranth = 0;
int right_paranth = 0;
int comma = 0;
int period = 0;
int exclamation = 0;
int question_mark = 0;
int asterisk = 0;
int vowels = 0;
int numbers = 0;
//objects of scanner class
Scanner filereader,sc;
filereader = new Scanner(new File("Chp5.txt"));
//input file reader
while(filereader.hasNext())
{
sc=filereader.useDelimiter("");
ch=filereader.next();
//punctuation
if(ch.equals("("))
{
left_paranth++;
punctuations++;
}
else if(ch.equals(")"))
{
right_paranth++;
punctuations++;
}
else if(ch.equals(","))
{
comma++;
punctuations++;
}
else if(ch.equals("."))
{
period++;
punctuations++;
}
else if(ch.equals("?"))
{
question_mark++;
punctuations++;
}
else if(ch.equals("*"))
{
asterisk++;
punctuations++;
}
// vowels
if(ch == 'a' || ch == 'A')
{
this.vowels += 1;
}
else if(ch == 'e' || ch == 'E')
{
this.vowels += 1;
}
else if(ch == 'i' || ch == 'I')
{
this.vowels += 1;
}
else if(ch == 'o' || ch == 'O')
{
this.vowels += 1;
}
else if(ch == 'u' || ch == 'U')
{
this.vowels += 1;
}
//numbers
if(Character.isDigit(ch))
{
this.numbers+=1;
}
//sc.close(); <-- I'm not sure if I need this?
}
filereader.close();
System.out.println("Total punctuations: " + punctuations);
System.out.println("Left paranthesis: " + left_paranth);
System.out.println("Right paranthesis: " + right_paranth);
System.out.println("Comma's: " + comma);
System.out.println("Period's: " + period);
System.out.println("Question marks: " + question_mark);
System.out.println("Asterick's: " + asterisk);
System.out.println("Vowel's: " + this.vowels);
System.out.println("Number's: " + this.numbers);
}
}
Explanation / Answer
Errors:
1. Using "this" in main method where variables are not class members.
2. Comparing String "ch" with characters.
Logical errors:
1. comparing entire line "ch" with punctuations.
Modified Code:
import java.io.File;
import java.util.Scanner;
import java.io.*;
public class TestB
{
public static void main(String[] args) throws IOException
{
String ch;
int punctuations=0;
//variables
int left_paranth = 0;
int right_paranth = 0;
int comma = 0;
int period = 0;
int exclamation = 0;
int question_mark = 0;
int asterisk = 0;
int vowels = 0;
int numbers = 0;
//objects of scanner class
Scanner filereader,sc;
filereader = new Scanner(new File("Chp5.txt"));
//input file reader
while(filereader.hasNext())
{
sc=filereader.useDelimiter("");
ch=filereader.next();
for(int i=0; i<ch.length(); i++)
{
//punctuation
if(ch.charAt(i) == '(')
{
left_paranth++;
punctuations++;
}
else if(ch.charAt(i) == ')')
{
right_paranth++;
punctuations++;
}
else if(ch.charAt(i) == ',')
{
comma++;
punctuations++;
}
else if(ch.charAt(i) == '.')
{
period++;
punctuations++;
}
else if(ch.charAt(i) == '?')
{
question_mark++;
punctuations++;
}
else if(ch.charAt(i) == '*')
{
asterisk++;
punctuations++;
}
else if(ch.charAt(i) == '!')
{
exclamation++;
punctuations++;
}
// vowels
if(ch.charAt(i) == 'a' || ch.charAt(i) == 'A')
{
vowels += 1;
}
else if(ch.charAt(i) == 'e' || ch.charAt(i) == 'E')
{
vowels += 1;
}
else if(ch.charAt(i) == 'i' || ch.charAt(i) == 'I')
{
vowels += 1;
}
else if(ch.charAt(i) == 'o' || ch.charAt(i) == 'O')
{
vowels += 1;
}
else if(ch.charAt(i) == 'u' || ch.charAt(i) == 'U')
{
vowels += 1;
}
//numbers
if(Character.isDigit(ch.charAt(i)))
{
numbers+=1;
}
}
//you don't need this as you are not using sc
//sc.close(); <-- I'm not sure if I need this?
}
filereader.close();
System.out.println("Total punctuations: " + punctuations);
System.out.println("Left paranthesis: " + left_paranth);
System.out.println("Right paranthesis: " + right_paranth);
System.out.println("Comma's: " + comma);
System.out.println("Period's: " + period);
System.out.println("Question marks: " + question_mark);
System.out.println("Asterick's: " + asterisk);
System.out.println("Exclamantions: " + exclamation);
System.out.println("Vowel's: " + vowels);
System.out.println("Number's: " + numbers);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.