Zerenity :
The "main menu" is the place where System.out.print("enter letter P T L S A or Q");
. When the user has entered his/her surname, the console should automatically return to the first System.out.print statement i.e the menu. How do I set it up to be like that?
class myOwnTryAginLoopThatWorks{
public static void main (String [] args){
Scanner console = new Scanner(System.in);
boolean valid;
char choice = '\0';
String persnr;
do{
valid = true;
System.out.print("enter letter P T L S A or Q"); //menu
String input = console.nextLine();
if (!isValid(input)){
valid = false;
System.out.print("You did not enter the correct menu option, ");
if (input.length() != 1) {
System.out.println("and your input length is too long (must be 1 character long), ");
valid = false;
}
}
choice = input.charAt(0);
}while(!valid);
switch (choice) {
case 'P':
do {
valid = true;
System.out.print("Enter persnr in the format of (YYYYMMDD): ");
try {
persnr = console.nextLine();
if (!persnr.matches("[1-9]{1}[0-9]{7}")) {
throw new IllegalArgumentException("You printed wrong format, try again");
}
System.out.println("Processsing...");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
valid = false;
}
} while (!valid);
break;
}
System.out.print("enter first name ");
String firstName = console.nextLine();
System.out.print("enter surrname ");
String surName = console.nextLine();
}
public static boolean isValid (String n){
switch(n){
case "P":
case "T":
case "L":
case "S":
case "A":
case "Q":
return true;
default:
return false;
}
}
}
Adonis González :
You MUST ask the question better. As I understand it, you need your menu is shown again asking to enter another data.
private static boolean exit = false;
public static void main(String[] args) {
while (exit == false){
readValues();
}
}
private static boolean isValid(String n) {
switch (n) {
case "P":
case "T":
case "L":
case "S":
case "A":
case "Q":
return true;
default:
return false;
}
}
private static void readValues() {
Scanner console = new Scanner(System.in);
boolean valid;
char choice = '\0';
String persnr;
do {
valid = true;
System.out.print("enter letter P T L S A or Q"); //menu
String input = console.nextLine();
if (!isValid(input)) {
valid = false;
System.out.print("You did not enter the correct menu option, ");
if (input.length() != 1) {
System.out.println("and your input length is too long (must be 1 character long), ");
valid = false;
}
}
choice = input.charAt(0);
} while (!valid);
switch (choice) {
case 'P':
do {
valid = true;
System.out.print("Enter DATE in the format of (YYYYMMDD): ");
try {
persnr = console.nextLine();
if (!persnr.matches("[1-9]{1}[0-9]{7}")) {
throw new IllegalArgumentException("You printed wrong format, try again");
}
System.out.println("Processsing...");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
valid = false;
}
} while (!valid);
break;
}
System.out.print("enter first name ");
String firstName = console.nextLine();
System.out.print("enter surrname ");
String surName = console.nextLine();
String query = " insert into Person (PNr, FName, ENamn)"
+ " values (persnr, firstName, surName)";
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=291060&siteId=1