loops - Java: How do I return user to main menu if user input is not a single alphabetic lowercase letter? -
i'm trying run program allow user input both char keycharacter , string thestring. then, using these inputs, mask keycharacter if occurs in thestring "$", remove keycharacter thestring, , finally, count number of times keycharacter occurs in thestring altogether.
every method working fine, except method getkeycharacter user has input char: user can enter single letter (e.g. q, or z).
if user enters other single letter (which can word, phrase, sentence, special character # or $, blank space or tabs, or pressing enter), program returns user original question asks keycharacter user. should continue looping original question until user enters valid input.
since i'm still beginner java , loops weakness far, part causing me lot of trouble. know should using while loop, logic behind nested loops confusing me.
from searching possible solutions, know there these things called regex , try-catch exception issue, since haven't gone on explicitly in class, i'd prefer not deal now. thank you.
here's paste of 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); } // 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 single character act key: "; system.out.print(firstprompt); strkeycharacter = inputstream.nextline(); while (strkeycharacter.length() != 1) { firstprompt = "please enter single character act key: "; system.out.print(firstprompt); strkeycharacter = inputstream.nextline(); } keycharacter = strkeycharacter.charat(0); while (strkeycharacter.length() == 1) { firstprompt = "please enter single character act 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 constant public static final int minimumlength = 4; // thestring public static string getstring() { scanner inputstream = new scanner(system.in); string secondprompt, thestring; { secondprompt = "please enter 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 $ public static string maskcharacter(string thestring, char keycharacter) { thestring = thestring.replace(keycharacter, '$'); system.out.println("string " + " '" + 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; (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 thestring public static void removecharacter(string thestring, char keycharacter) { thestring = thestring.replace(string.valueof(keycharacter), ""); system.out.println("string " + "'" + keycharacter + "' removed: "); system.out.println(thestring); return; } }
and here's paste of run (as can see, there serious debugging done in program):
please enter single character act key: f please enter single character act key: f entered: f please enter single character act key: f entered: f please enter single character act key: f entered: f please enter single character act key: f entered: f please enter single character act key: // continues on forth...
public static char getkeycharacter(){ scanner inputstream = new scanner(system.in); boolean stop = false; string firstprompt, strkeycharacter; char keycharacter = ' '; while(!stop){ firstprompt = "please enter single character act key: "; system.out.println(firstprompt); strkeycharacter = inputstream.nextline(); //check if input contains 1 character boolean issinglechar = (strkeycharacter.length() == 1); //check if input character within ascii code of 97 (a) 122 (z) boolean isvalidchar = strkeycharacter.charat(0) >= 97 && strkeycharacter.charat(0) <= 122; if(issinglechar && isvalidchar){ keycharacter = strkeycharacter.charat(0); stop = true; } } return keycharacter; }
Comments
Post a Comment