validation - Java console input handling -
this first question here, hope it's not based on opinions. i've searched on internet quite while now, couldn't find similar question.
need write java program reads commands console, validates input, gets parameters , passes them on different class.
there restrictions on can , use (university).
- only packages java.util, java.lang , java.io allowed
- each method can 80 lines long
- each line can 120 characters long
- i not allowed use system.exit / runtime.exit
- the terminal class used handle user input.
terminal.readline()
read line console,scanner.nextline()
i have working program - solution not accepted because of way handle console inputs (runinteractionloop()
method long). i'm doing this:
the main class has main method , "interaction loop" console inputs handled. main method calls interaction loop in while loop, boolean "quit" guardian.
private static boolean quit = false; ... public static void main(string[] args) { ... while (quit == false) { runinteractionloop(); } }
the interaction loop handles console input. need check 16 different commands - each own types of parameters. chose work patterns , matchers, because can use groups convenience. problems start - have never learned how correctly handle user inputs. have done here is, each possible command, create new matcher, see if input matches, if whatever needs done input.
private static runinteractionloop() { matcher m; string query = terminal.readline; m = pattern.compile("sliding-window (\\d+) (-?\\d+(?:\\.\\d+)?;)*(-?\\d+(?:\\.\\d+)?)").matcher(query); if (m.matches()) { xyz.dosth(integer.parseint(m.group(1)), ......); ... return; } m = pattern.compile("record ([a-z]+) (-?\\d+(?:\\.\\d+)?)").matcher(query); if (m.matches()) { xyz.dosthelse(m.group(1), double.parsedouble(m.group(2))); return; } ... if (query.equals("quit")) { quit = true; return; } terminal.printerror("invalid input"); }
as can see, doing 16 times stretches out method more 80 lines (5 lines per input max). it's inefficient , honest, i'm quite ashamed posting here (crap code). don't know how correctly, using java.util , having way parameters (e.g. matcher groups here).
ideas? grateful suggestions. thanks.
edit/update:
have made decision split verification 2 methods - 1 each half of commands. looks ugly, passes uni's checkstyle requirements. however, i'd still more happy if shows me better solution problem - future (because have no idea how make prettier, shorter and/or more efficient).
i guess try painful separate chain of method calls:
private static runinteractionloop() { matcher m; string query = terminal.readline; m = pattern.compile("sliding-window (\\d+) (-?\\d+(?:\\.\\d+)?;)*(-?\\d+(?:\\.\\d+)?)").matcher(query); if (m.matches()) { xyz.dosth(integer.parseint(m.group(1)), ......); ... return; } else { trydouble(query, m); } } private static trydouble(string query, matcher m) { m = pattern.compile("record ([a-z]+) (-?\\d+(?:\\.\\d+)?)").matcher(query); if (m.matches()) { xyz.dosthelse(m.group(1), double.parsedouble(m.group(2))); return; } else { trysomethingelse(query, m); } } private static trysomethingelse(string query, matcher m) { ... if (query.equals("quit")) { quit = true; return; } terminal.printerror("invalid input"); }
Comments
Post a Comment