I've been wondering for a moment, is it possible to create a dynamic do-while statement in Java (with function/method)?
I've been working around, and have this 'semi-automatic' function work:
import java.util.*;
public class Test{
Scanner inp = new Scanner(System.in);
Test(){
String name = "", any = "";
int num = 0;
System.out.println("Static 1");
//the 'semi-automatic' do while sample
doWhile("Input name (String): ", name, 3, 10);
doWhile("Input number (Integer): ", num, 1, 17);
//another do while statement; I know this won't work
doWhile("Input anything but numeric: ", any, !any.matches("[A-Za-z]+"));
}
public static void main(String[] args){
new Test();
}
void doWhile(String a, String b, boolean c){
do{
System.out.print(a);
b = inp.nextLine();
} while(c);
}
//do-while string
void doWhile(String a, String b, int c, int d){
do{
System.out.print(a);
b = inp.nextLine();
} while(b.length() < c || b.length() > d);
}
//do-while int
void doWhile(String a, int b, int c, int d){
do{
System.out.print(a);
try{
b = inp.nextInt();
} catch(Exception e){
System.out.println("Please input accordingly");
}
inp.nextLine();
} while(b < c || b > d);
}
}
I know that the problem must be came from 'while' value, which I have ran out of ideas on how to make it fully dynamic. Any ideas (or perhaps alternative)?