Skip to content

Instantly share code, notes, and snippets.

@Pzdrs
Created February 24, 2022 19:39
Show Gist options
  • Select an option

  • Save Pzdrs/029edcee5f547bcd8c2320f0d927ab93 to your computer and use it in GitHub Desktop.

Select an option

Save Pzdrs/029edcee5f547bcd8c2320f0d927ab93 to your computer and use it in GitHub Desktop.
Prompt the user for a string until empty line is passed in
/**
* Prompt the user for a string until empty line is passed in
*
* @param consumer A consumer interface with a piece of code that is suppose to be ran on each iteration
*/
public static void readLineUntilEmpty(Consumer<String> consumer) {
String line;
do {
System.out.print("Enter a string: ");
line = Main.scanner.nextLine();
if (!line.isEmpty()) consumer.accept(line);
} while (!line.equals(""));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment