So, I want to receive input from the user, check if they used alphabetical values and then check if it is too long. If too long, I want to start again from the top (checking if alphabetical) by calling the method I am in. However, when I start over and I type, say “Danny”, this will show:
Output: “Thank you, got Danny” Output: (length of previous, too long input) + “is too many characters, try to keep it under 30.”
So somehow, it keeps the original input (that was alphabetical, but above 30) saved and it doesn’t alter it when it starts over. Anyone know what I should do instead?
public static String inputPattern() { Scanner scanner = new Scanner(System.in); String player; int strLength; System.out.println("Please enter your name:"); while (!scanner.hasNext("[A-Za-z]+")) { //Checks if alphabetical value System.out.println("Please stick to the alphabet!"); scanner.next(); } player = scanner.next(); player += scanner.nextLine(); System.out.println("Thank you! Got " + player); strLength = player.length(); // Saves the length of user-inputted name while (strLength > 30) { // Checks if not too long System.out.println(strLength + " is too many characters, please try to keep it under 30"); inputPattern(); // Starts over again if too long } return player; }
Advertisement
Answer
I have taken your method and modified it a bit.
It is non recursive solution.
Also in your code scanner resource was not closed at the end.
Iterative Solution
import java.util.Scanner; public class SO66064473 { public static void main(String[] args) { inputPatternIterative(); } public static String inputPatternIterative() { Scanner scanner = new Scanner(System.in); String player = ""; int strLength = Integer.MAX_VALUE; while (strLength > 30) { // Checks if not too long System.out.println("Please enter your name:"); while (!scanner.hasNext("[A-Za-z]+")) { //Checks if alphabetical value System.out.println("Please stick to the alphabet!"); scanner.next(); } player = scanner.next(); player += scanner.nextLine(); System.out.println("Thank you! Got " + player); strLength = player.length(); // Saves the length of user-inputted name if (strLength > 30) System.out.println(strLength + " is too many characters, please try to keep it under 30"); } scanner.close(); // Closing scanner resource after use. return player; } }
Output :
Please enter your name: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Thank you! Got aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 70 is too many characters, please try to keep it under 30 Please enter your name: aaaaaaaaaaaaaaaaaaaa12 Please stick to the alphabet! coifvoifoivmrfvoirvoirovroijfoirjfoijroifjrwofjorwfouwrfoijwrofjworjfoiwrjf Thank you! Got coifvoifoivmrfvoirvoirovroijfoirjfoijroifjrwofjorwfouwrfoijwrofjworjfoiwrjf 75 is too many characters, please try to keep it under 30 Please enter your name: Danny Thank you! Got Danny
EDIT : with the suggestion made by @Dev-vruper here is updated easy recursive code
Recursive Solution
import java.util.Scanner; public class SO66064473 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); inputPatternRecursive(sc); sc.close(); } public static String inputPatternRecursive(Scanner sc) { System.out.println("Please enter your name:"); String player = sc.nextLine(); if (!player.matches("[A-Za-z]+")) { System.out.println("Please stick to the alphabet!"); inputPatternRecursive(sc); } else { System.out.println("Thank you! Got " + player); if (player.length() > 30) { System.out.println(player.length() + " is too many characters, please try to keep it under 30"); inputPatternRecursive(sc); } } return player; } }