I changed my code as Dan suggested i can compile the program now, however, whatever the input is, the result is always 2. i put the second part of this program below the new code. please help.
Here’s the NEW code.
public class VowelCons
{
private final String str;
private final int totalConsonants;
private final int totalVowels;
public VowelCons(final String s)
{
this.str = s;
int totalConsonants = 0;
int totalVowels = 0;
if (null != s)
{
for (final char c : s.toCharArray())
{
switch (c)
{
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':
totalVowels++;
break;
default:
if (Character.isLetter(c))
{
totalConsonants++;
}
break;
}
}
}
this.totalConsonants = totalConsonants;
this.totalVowels = totalVowels;
}
public String getString()
{
return str;
}
public int getNumConsonants()
{
return this.totalConsonants;
}
public int getNumVowels()
{
return this.totalConsonants;
}
}
there’s another part of this program which gets the user’s input and passes it to this class.
Here’s the code. [this part cannot be changed according to the regulations]
import java.util.Scanner;
public class VowelConsCounter
{
public static void main(String[] args)
{
String input; // User input
char selection; // Menu selection
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a string: ");
input = keyboard.nextLine();
VowelCons vc = new VowelCons(input);
do
{
selection = getMenuSelection();
switch(Character.toLowerCase(selection))
{
case 'a' : System.out.println("\nNumber of vowels: " +
vc.getNumVowels());
break;
case 'b' : System.out.println("\nNumber of consonants: " +
vc.getNumConsonants());
break;
case 'c' : System.out.println("\nNumber of vowels: " +
vc.getNumVowels());
System.out.println("Number of consonants: " +
vc.getNumConsonants());
break;
case 'd' : System.out.print("Enter a string: ");
input = keyboard.nextLine();
vc = new VowelCons(input);
}
} while (Character.toLowerCase(selection) != 'e');
}
public static char getMenuSelection()
{
String input;
char selection;
Scanner keyboard = new Scanner(System.in);
System.out.println("a) Count the number of vowels in the string.");
System.out.println("b) Count the number of consonants in the string.");
System.out.println("c) Count both the vowels and consonants in the string.");
System.out.println("d) Enter another string.");
System.out.println("e) Exit the program.");
input = keyboard.nextLine();
selection = input.charAt(0);
while (Character.toLowerCase(selection) < 'a' || Character.toLowerCase(selection) > 'e')
{
System.out.print("Only enter a, b, c, d, or e: ");
input = keyboard.nextLine();
selection = input.charAt(0);
}
return selection;
}
}
You’re getting a
NullPointerExceptionbecause you are not initializing the instance variableresult.I would recommend using the following:
This would, for example, output:
Here are some points this example should help you learn:
thisto reference instance variables (see [1]). You should always reference instance variables usingthis, which prevents accidentally hiding it when you make future code changes and allows IDEs to provide context sensitive suggestions that only contain instance members.nullStrings passed to the constructor.switchto simplify the logic and reduce redundant code inif-elselogic.toString().