Reputation: 123
I've been following Tony Gaddis's programming challenges in his book and I've tried doing the "vowels and consonants program". The program will ask the user to input any string, and then it will count the number of vowels or consonants within that string. I was able to code most of it but there has been a problem with the output. Hope you guys can help me with this one.
The problem is like this:
************************|
1 - Count Vowels
2 - Count Consonants
3 - Exit
Enter mode:1
****VOWELS****
Enter words:abracadabra
NUMBER OF VOWELS: 5
Do you want to input again(y/n):n
************************|
1 - Count Vowels
2 - Count Consonants
3 - Exit
Enter mode:1
(and then it'll just end here)
First, I chose mode 1 for counting vowels and then if typed in any string. Then, it did successfully count the number of vowels and it will ask me if i want to input again, I've typed in n for no. The code went back to choosing modes and then, I enter 1 again to count for vowels. This time, it did nothing. It just stopped, where supposedly I should enter a string again and etc. The same thing happens also when I choose 2 for counting consonants.
Here is my code:
PART 1:
package Test;
public class VowelMain {
private String words;
private int vowel = 0;
public VowelMain(String w){
words = w;
setVowel(words);
}
public void setVowel(String words){
char[]chunks = words.toCharArray();
for(int x = 0;x < chunks.length;x++){
if(chunks[x]=='a' || chunks[x]=='e' || chunks[x]=='i' || chunks[x]=='o' || chunks[x]=='u'){
vowel += 1;
}
}
}
public int getVowel(){
return vowel;
}
}
PART II:
package Test;
public class ConsonantMain {
private String words;
private int consonant = 0;
public ConsonantMain(String w){
words = w;
setConsonant(words);
}
public void setConsonant(String words){
char[]chunks = words.toCharArray();
for(int x = 0;x < chunks.length;x++){
if(chunks[x]=='a' || chunks[x]=='e' || chunks[x]=='i' || chunks[x]=='o' || chunks[x]=='u'){
}else{
consonant += 1;
}
}
}
public int getConsonant(){
return consonant;
}
}
PART III:
package Test;
import java.util.Scanner;
public class TestMain {
public static void main(String[]args){
int mode = getMode();
operation(mode);
}
public static void operation(int mode){
Scanner hold = new Scanner(System.in);
String words;
String response;
while(mode == 1 || mode == 2 || mode == 3){
switch(mode){
case 1:
System.out.print("****VOWELS****\n");
do{
System.out.print("Enter words:");
words = hold.nextLine();
VowelMain t = new VowelMain(words);
System.out.println("NUMBER OF VOWELS: " + t.getVowel());
System.out.print("Do you want to input again(y/n):");
response = hold.nextLine();
if(response.equalsIgnoreCase("n")){
mode = -1;
getMode();
}
}while(response.equalsIgnoreCase("y"));
break;
case 2:
System.out.print("****CONSONANTS****\n");
do{
System.out.print("Enter words:");
words = hold.nextLine();
ConsonantMain c = new ConsonantMain(words);
System.out.println("NUMBER OF CONSONANTS: " + c.getConsonant());
System.out.print("Do you want to input again(y/n):");
response = hold.nextLine();
if(response.equalsIgnoreCase("n")){
mode = -1;
getMode();
}
}while(response.equalsIgnoreCase("y"));
break;
case 3:
System.exit(0);
break;
default:
System.out.println("ERROR!");
break;
}
}
}
public static int getMode(){
Scanner hold = new Scanner(System.in);
int mode;
System.out.println("************************");
System.out.println("1 - Count Vowels");
System.out.println("2 - Count Consonants");
System.out.println("3 - Exit");
System.out.print("Enter mode:");
mode = hold.nextInt();
return mode;
}
}
Upvotes: 0
Views: 731
Reputation: 2291
Your function is terminating after a loop when we input "n"
and invokes the getMode
function which is needed here after the user inputs "n"
.
All you have to do is remove the line getMode()
from your if
statement in your case 2:
and case 3:
as shown below:
if(response.equalsIgnoreCase("n")){
mode = -1;
getMode();
}
and change it to,
if(response.equalsIgnoreCase("n")){
mode = getMode();
}
Upvotes: 0
Reputation: 557
Each time you put
mode = -1;
getMode();
You should replace it with:
mode = getMode();
Here's why. As RealSkeptic said, getMode()
is returning a temporary mode
value, not the mode
value being used in your loop. By assigning the loop mode
to the temporary mode (through the return statement) your code will work fine.
Upvotes: 1