/** * Returns a non-empty vector of integers in the range min..max. Numbers in the input may be * delimited by whitespaces and/or commas. If no input is given, a vector with 1 random value in * the range min..max is returned. */ Vector<Integer> getNonEmptyVI(int min, int max) { final Vector<Integer> args = getVI(); if (args.size() == 0) { args.add(MyRandom.Int(min, max)); sb.setText("no input; using random value"); } return args; }
/** * Returns an integer in the range min..max. If no input is given, a default value def is * returned. */ public int getInt(int def, int min, int max) { int n = def; final String firstWord = this.getText().split("(\\s|,)")[0]; try { n = Integer.parseInt(firstWord); if (n < min) { n = min; sb.setText("value too small; using the minimum value " + min + " instead"); } if (n > max) { n = max; sb.setText("value too high; using the maximum value " + max + " instead"); } sb.setText(" "); } catch (final NumberFormatException e) { sb.setText("couldn't parse an integer; using the default value " + def); } setText(""); return n; }
/** * Returns a vector of integers in the range min..max. Numbers in the input may be delimited by * whitespaces and/or commas. */ public Vector<Integer> getVI(int min, int max) { boolean range = false; final Vector<Integer> args = new Vector<Integer>(); final String[] tokens = this.getText().replaceAll("\\.{2,}", " .. ").split("(\\s|,)+"); for (final String t : tokens) { if ("..".equals(t)) { range = true; } else { int x = min; try { x = Integer.parseInt(t); if (x < min) { x = min; sb.setText("value too small; using the minimum value instead"); } if (x > max) { x = max; sb.setText("value too high; using the maximum value instead"); } if (range) { final int a = args.lastElement(); for (int i = a + 1; i < x; ++i) { args.add(i); } for (int i = a - 1; i > x; --i) { args.add(i); } range = false; } args.add(x); } catch (final NumberFormatException e) { sb.setText("couldn't parse an integer"); } } } setText(""); return args; }