Java: How to return the user to the main menu if the user input is not a single lowercase alphabetic?

I am trying to run a program that will allow the user to enter both char keyCharacter and String theString. Then, using those inputs, I will mask the keyCharacter if it occurs in a String with "$", remove the keyCharacter from theString, and finally count the number of times the keyCharacter occurs altogether in the String.

Every method works fine, except for the getKeyCharacter method where the user must enter a char: The user can only enter one letter (like q or z).

If the user enters anything other than that single letter (which can be anything from a word, phrase, sentence, special character like # or $, space or tabs, or just pressing enter), then the program returns the user to the original question. which asks for a KeyCharacter from the user. This should continue going back to this original question until the user enters valid input.

Since I'm still new to java and loops are my weakness, this part is giving me tons of problems. I know I have to use a while loop, the logic behind nested loops really confuses me.

From looking for possible solutions, I know that there are things like regex and try-catch exception that might help with my problem, but since we didn't go over this explicitly in the class, I would rather not tackle that at the moment. Thank.

Here's a paste of my code:

import java.util.*;

public class Foothill {

    // main method
    public static void main (String[] args) {
        char keyCharacter = getKeyCharacter();
        String theString = getString();    
        maskCharacter(theString, keyCharacter);
        countKey(theString, keyCharacter);
        removeCharacter(theString, keyCharacter);
    }

    // get keyCharacter
    public static char getKeyCharacter() {
        Scanner inputStream = new Scanner(System.in);
        boolean stop = false;
        String firstPrompt, strKeyCharacter;
        char keyCharacter = ' ';

        while (stop != true) {
            firstPrompt = "Please enter a SINGLE character to act as key: ";
            System.out.print(firstPrompt);
            strKeyCharacter = inputStream.nextLine(); 

            while (strKeyCharacter.length() != 1) {
                firstPrompt = "Please enter a SINGLE character to act as key: ";
                System.out.print(firstPrompt);
                strKeyCharacter = inputStream.nextLine();
            }

            keyCharacter = strKeyCharacter.charAt(0);

            while (strKeyCharacter.length() == 1) {
                firstPrompt = "Please enter a SINGLE character to act as key: ";
                System.out.print(firstPrompt);
                strKeyCharacter = inputStream.nextLine();
                if (keyCharacter == 'a' || keyCharacter == 'b' || keyCharacter == 'c' || keyCharacter == 'd' 
                  || keyCharacter == 'e' || keyCharacter == 'f' || keyCharacter == 'g' || keyCharacter == 'h'
                  || keyCharacter == 'i' || keyCharacter == 'j' || keyCharacter == 'k' || keyCharacter == 'l'
                  || keyCharacter == 'm' || keyCharacter == 'n' || keyCharacter == 'o' || keyCharacter == 'p'
                  || keyCharacter == 'q' || keyCharacter == 'r' || keyCharacter == 's' || keyCharacter == 't'
                  || keyCharacter == 'u' || keyCharacter == 'v' || keyCharacter == 'w' || keyCharacter == 'x'
                  || keyCharacter == 'y' || keyCharacter == 'z') {
                    System.out.println("You entered: " + keyCharacter + '\n');
                    stop = true;
                } else {
                    break;
                }
            }
        }
        return keyCharacter;
    }

    // declare final = 4 to be constant
    public static final int minimumLength = 4;

    // get theString
    public static String getString() {
        Scanner inputStream = new Scanner(System.in);
        String secondPrompt, theString;
        do {
            secondPrompt = "Please enter a phrase or sentence >= 4: ";
            System.out.print(secondPrompt);
            theString = inputStream.nextLine();
            System.out.print('\n');
        } while (theString.length() < minimumLength || theString == null || theString.length() == 0);
        inputStream.close();
        return theString;
    }

    // mask keyCharacter with $
    public static String maskCharacter(String theString, char keyCharacter) {
        theString = theString.replace(keyCharacter, '$');
        System.out.println("String with " + " '" + keyCharacter + "' " + " masked.");
        System.out.println(theString + '\n');
        return theString;
    }

    // count number of times keyCharacter occurs in theString
    public static void countKey(String theString, char keyCharacter) {
        int countChar = 0;
        for (int charTimes = 0; charTimes < theString.length(); charTimes++) {
            if (theString.charAt(charTimes) == keyCharacter) {
                countChar++;
            }
        }
        System.out.println( "The key character occurs " + countChar + " times. \n");
        return;
    }

    // remove keyCharacter from theString
    public static void removeCharacter(String theString, char keyCharacter) {
        theString = theString.replace(String.valueOf(keyCharacter), "");
        System.out.println("String with " + "'" + keyCharacter + "' removed: ");
        System.out.println(theString);
        return;
    }
}

      

And here is the paste of my startup (as you can see, there is some serious debugging in my program):

Please enter a SINGLE character to act as key: f
Please enter a SINGLE character to act as key: f
You entered: f

Please enter a SINGLE character to act as key: f
You entered: f

Please enter a SINGLE character to act as key: f
You entered: f

Please enter a SINGLE character to act as key: f
You entered: f

Please enter a SINGLE character to act as key: 

// which then continues so on so forth...

      

+3


source to share


1 answer


public static char getKeyCharacter(){

    Scanner inputStream = new Scanner(System.in);
    boolean stop = false;
    String firstPrompt, strKeyCharacter;
    char keyCharacter = ' ';

    while(!stop){

        firstPrompt = "Please enter a SINGLE character to act as key: ";
        System.out.println(firstPrompt);
        strKeyCharacter = inputStream.nextLine();

        //check if the input contains only 1 character
        boolean isSingleChar = (strKeyCharacter.length() == 1);
        //check if the input character is within the ASCII code of 97 (a) to 122 (z)
        boolean isValidChar = 
                strKeyCharacter.charAt(0) >= 97 &&
                strKeyCharacter.charAt(0) <= 122;

        if(isSingleChar && isValidChar){
            keyCharacter = strKeyCharacter.charAt(0);
            stop = true;
        }


    }

    return keyCharacter;
}

      



+2


source







All Articles