/** * Returns date in the format: neededDatePattern, in case if year or month isn't entered, current * year/month is put. * * @return correct date. */ private Date getCorrectedDate(String enteredDate) { Queue<String> dateParts = new ArrayDeque<>(3); StringBuilder number = new StringBuilder(); for (char symbol : enteredDate.toCharArray()) { if (Character.isDigit(symbol)) { number.append(symbol); } else if (number.length() > 0) { dateParts.add(number.toString()); number = new StringBuilder(); } } if (number.length() > 0) { dateParts.add(number.toString()); } Calendar currentDate = Calendar.getInstance(); switch (dateParts.size()) { case 1: dateParts.add(Integer.toString(currentDate.get(Calendar.MONTH) + 1)); case 2: dateParts.add(Integer.toString(currentDate.get(Calendar.YEAR))); } try { return new SimpleDateFormat("dd.MM.yyyy") .parse(dateParts.remove() + '.' + dateParts.remove() + '.' + dateParts.remove()); } catch (ParseException e) { throw new RuntimeException(e); // todo change exception } }
static { String javaVersion = System.getProperty("java.version"); // Value is on the form M.N.U_P[-xxx] where M,N,U,P are decimal integers if (null != javaVersion) { int startPos = 0; int endPos = 0; int max = javaVersion.length(); while (endPos < max && Character.isDigit(javaVersion.charAt(endPos))) { endPos++; } if (startPos < endPos) { try { javaVersionMajor = Integer.parseInt(javaVersion.substring(startPos, endPos)); startPos = endPos + 1; endPos = startPos; while (endPos < max && Character.isDigit(javaVersion.charAt(endPos))) { endPos++; } if (startPos < endPos) { javaVersionMinor = Integer.parseInt(javaVersion.substring(startPos, endPos)); startPos = endPos + 1; endPos = startPos; while (endPos < max && Character.isDigit(javaVersion.charAt(endPos))) { endPos++; } if (startPos < endPos) { javaVersionMicro = Integer.parseInt(javaVersion.substring(startPos, endPos)); } } } catch (NumberFormatException _nfe) { } } } }
public boolean isPalindrome(String s) { if (s.length() == 0) return true; // System.out.println(cs); int i = 0, j = s.length() - 1; boolean flag = true; while (i < j) { // System.out.println(s.charAt(i)+" vs " + s.charAt(j) ); if (Character.isLetterOrDigit(s.charAt(i)) == false) { // System.out.println("i is not alphanumeric " + s.charAt(i) ); i++; continue; } if (Character.isLetterOrDigit(s.charAt(j)) == false) { // System.out.println("j is not alphanumeric:" + s.charAt(j) ); j--; continue; } if (s.charAt(i) == s.charAt(j) || Math.abs(s.charAt(i) - s.charAt(j)) == 32) { if ((Character.isDigit(s.charAt(i)) && Character.isLetter(s.charAt(j))) || (Character.isDigit(s.charAt(j)) && Character.isLetter(s.charAt(i)))) { flag = false; break; } flag = true; i++; j--; } else { flag = false; // System.out.println(s.charAt(i)+" vs " + s.charAt(j) ); break; } } System.out.println(flag); return flag; }
public static boolean isFormat(String str, String... format) { if (str.length() == 0) return format.length == 1 && format[0].equals(""); char[] charset = str.toCharArray(); int c = 0; for (int i = 0; i < format.length; i++) { String form = format[i]; if (form.equals(NUMBERS)) { if (c >= charset.length) return false; if (!Character.isDigit(charset[c])) { if (charset[c] == '.') { if (c + 1 >= charset.length || !Character.isDigit(charset[c + 1])) return false; } else return false; } while (c < charset.length - 1 && (Character.isDigit(charset[c + 1]) || charset[c + 1] == '.')) c++; c++; } else if (form.equals(LETTERS)) { if (c >= charset.length || !Character.isLetter(charset[c])) return false; while (c < charset.length - 1 && Character.isLetter(charset[c + 1])) c++; c++; } else { char[] formchars = form.toCharArray(); if (formchars.length > charset.length - c) return false; for (int ch = 0; ch < formchars.length; ch++) { if (formchars[ch] != charset[c]) return false; c++; } } } return c == charset.length; }
int number() { if (!Character.isDigit(cs[p])) return 1; int res = 0; while (Character.isDigit(cs[p])) { res *= 10; res += cs[p++] - '0'; } return res; }
public static int add(String line) { if (line.isEmpty()) return 0; String number = ""; int i = 0; int startIndex = 0; int endIndex = 0; for (i = 0; i < line.length(); i++) if (Character.isDigit(line.charAt(i))) startIndex = i; for (; i < line.length() && Character.isDigit(line.charAt(i)); i++) ; endIndex = i; number = line.substring(--startIndex, endIndex); return Integer.parseInt(number) + add(line.substring(endIndex)); }
/** * Modtager værtens svar, der godt kan løbe over flere linjer. Sidste linje er en svarkode på tre * cifre, uden en bindestreg '-' på plads nummer 4 */ private String læsSvar() throws IOException { while (true) { String s = ind.readLine(); System.out.println("modt: " + s); if (s.length() >= 3 && s.charAt(3) != '-' && Character.isDigit(s.charAt(0)) && Character.isDigit(s.charAt(1)) && Character.isDigit(s.charAt(2))) return s; // afslut løkken og returner sidste linje med statuskode } }
static int nextInt() throws IOException { int ch = -1; do { ch = System.in.read(); } while (!Character.isDigit(ch)); int ret = 0; while (Character.isDigit(ch)) { ret *= 10; ret += Character.digit(ch, 10); ch = System.in.read(); } return ret; }
public static String urlEscape(String param) { String urlOK = ""; StringBuilder sb = new StringBuilder(); for (int i = 0; i < param.length(); ++i) { char ch = param.charAt(i); char lowerCh = Character.toLowerCase(ch); if (Character.isDigit(ch) || (-1 != "[email protected]".indexOf(lowerCh))) { sb.append(ch); } else if (' ' == ch) { sb.append('+'); } else if ((0x7F & ch) == ch) { putCh(sb, ch); } else if ((0xFFF & ch) == ch) { putCh(sb, 0xD0 | (ch >> 6)); putCh(sb, 0x80 | (0x3F & ch)); } else { putCh(sb, 0xE8 | (ch >> 12)); putCh(sb, 0x80 | (0x3F & (ch >> 6))); putCh(sb, 0x80 | (0x3F & ch)); } } return sb.toString(); }
/** Loads custom hyphenation rules. You probably want to use loadDefault() instead. */ public void load(BufferedReader input) throws IOException { String line; while ((line = input.readLine()) != null) { if (StringUtils.matches(line, "\\s*(%.*)?")) { // comment or blank line System.err.println("Skipping: " + line); continue; } char[] linechars = line.toCharArray(); int[] pattern = new int[linechars.length]; char[] chars = new char[linechars.length]; int c = 0; for (int i = 0; i < linechars.length; ++i) { if (Character.isDigit(linechars[i])) { pattern[c] = Character.digit(linechars[i], 10); } else { chars[c++] = linechars[i]; } } char[] shortchars = new char[c]; int[] shortpattern = new int[c + 1]; System.arraycopy(chars, 0, shortchars, 0, c); System.arraycopy(pattern, 0, shortpattern, 0, c + 1); insertHyphPattern(shortchars, shortpattern); } }
public Date getDate(FunctionQParser fp, String arg) { if (arg == null) return null; if (arg.startsWith("NOW") || (arg.length() > 0 && Character.isDigit(arg.charAt(0)))) { return df.parseMathLenient(null, arg, fp.req); } return null; }
public StringBuffer translate() { for (index = 0; index < line.length(); index++) { char c = line.charAt(index); if (Character.isDigit(c)) { dealWithOperand(); } else if (isOperator(c)) { dealWithOperator(c); } else if (c == '(') { stack.push(new Character(c)); } else if (c == ')') { dealWithCloser(); } else if (Character.isSpaceChar(c)) { // do nothing } else { System.out.println("Error: unknown character" + c); } } // pop and output all the operators left on the stack while (!stack.empty()) { out.append(popChar()); } return out; }
public int cal(String input) throws SyntaxErrorException { // initiates the empty stack operands = new Stack<Integer>(); // tokenizes the input by space String[] tokens = input.split("\\s+"); try { for (String eachToken : tokens) { char first = eachToken.charAt(0); if (Character.isDigit(first)) { int value = Integer.parseInt(eachToken); operands.push(value); } else if (isOperator(first)) { int result = calOps(first); operands.push(result); } else { throw new SyntaxErrorException("Invalid character ! " + first); } } // end of for loop // the case of no more tokens - pop result from stack int answer = operands.pop(); if (operands.empty()) { return answer; } else { throw new SyntaxErrorException("Stack is not empty..."); } } catch (EmptyStackException ex) { throw new SyntaxErrorException("It attempts to pop the empty stack .. "); } }
static ReferenceType getReferenceTypeFromToken(String idToken) { ReferenceType cls = null; if (Character.isDigit(idToken.charAt(0))) { cls = null; } else if (idToken.startsWith("*.")) { // This notation saves typing by letting the user omit leading // package names. The first // loaded class whose name matches this limited regular // expression is selected. idToken = idToken.substring(1); for (ReferenceType type : Env.vm().allClasses()) { if (type.name().endsWith(idToken)) { cls = type; break; } } } else { // It's a class name List<ReferenceType> classes = Env.vm().classesByName(idToken); if (classes.size() > 0) { // TO DO: handle multiples cls = classes.get(0); } } return cls; }
public static String getDigits(String s) { String str = ""; for (int i = 0; i < s.length(); i++) { if (Character.isDigit(s.charAt(i))) str += s.charAt(i); } return str; }
public static int makestring(String a, Token b) { char[] temp = new char[a.length()]; temp = a.toCharArray(); int i = 1; if (temp[0] == '\'') { while (Character.isLetter(temp[i]) || Character.isDigit(temp[i]) || isoperator(temp[i]) || ispunc(temp[i]) || Character.isWhitespace(temp[i])) { i++; if (temp[i - 1] == '\'') { setToken(b, i - 2, a.substring(1, i - 1), LexToInt.string); return i; } if (i == a.length()) { break; } } if (temp[i - 1] == '\'') { setToken(b, i - 2, a.substring(1, i - 1), LexToInt.string); return i; } } return -1; }
public static int makeint(String a, Token b) { char[] temp = new char[a.length()]; temp = a.toCharArray(); int i = 0; if (Character.isDigit(temp[i])) { while (Character.isDigit(temp[i])) { i++; if (i == a.length()) { break; } } setToken(b, i, a.substring(0, i), LexToInt.integer); return i; } return -1; }
/** Adds a sound to this sound pool. */ public SoundPoolEntry addSound(String par1Str, File par2File) { try { String s = par1Str; par1Str = par1Str.substring(0, par1Str.indexOf(".")); if (isGetRandomSound) { for (; Character.isDigit(par1Str.charAt(par1Str.length() - 1)); par1Str = par1Str.substring(0, par1Str.length() - 1)) {} } par1Str = par1Str.replaceAll("/", "."); if (!nameToSoundPoolEntriesMapping.containsKey(par1Str)) { nameToSoundPoolEntriesMapping.put(par1Str, new ArrayList()); } SoundPoolEntry soundpoolentry = new SoundPoolEntry(s, par2File.toURI().toURL()); ((List) nameToSoundPoolEntriesMapping.get(par1Str)).add(soundpoolentry); allSoundPoolEntries.add(soundpoolentry); numberOfSoundPoolEntries++; return soundpoolentry; } catch (MalformedURLException malformedurlexception) { malformedurlexception.printStackTrace(); throw new RuntimeException(malformedurlexception); } }
private Map<String, Object> retrieveObjectMap(Object object) throws Exception { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Map<String, Object> objectMap = new Hashtable<String, Object>(); Method method[] = object.getClass().getDeclaredMethods(); for (int i = 0; i < method.length; i++) { if (method[i].getName().startsWith("get")) { String tempKey = method[i].getName(); tempKey = tempKey.substring(tempKey.lastIndexOf(".") + 1); tempKey = tempKey.substring(3); String key = ""; for (int j = 0; j < tempKey.length(); j++) if (j == 0) key += tempKey.substring(0, 1).toLowerCase(); else if (Character.isUpperCase(tempKey.charAt(j))) key += ("_" + tempKey.substring(j, j + 1).toLowerCase()); else if (Character.isDigit(tempKey.charAt(j))) key += ("_" + tempKey.substring(j, j + 1).toLowerCase()); else key += tempKey.substring(j, j + 1); if (object.getClass().getName().endsWith("MagazineImage") && key.equals("magazine_id")) key = "focus_id"; Object value = method[i].invoke(object); if (value instanceof Date) value = simpleDateFormat.format((Date) value); if (value != null) objectMap.put(key, value); } } return objectMap; }
public String tickInfo(String which) { int grpstart = -1; for (int i = 0; i < which.length(); i++) if (Character.isDigit(which.charAt(i))) { grpstart = i; break; } if (which.equalsIgnoreCase("tickGroupSize")) return "" + ticks.size(); else if (which.toLowerCase().startsWith("tickerssize")) { if (grpstart < 0) return ""; int group = CMath.s_int(which.substring(grpstart)); if ((group >= 0) && (group < ticks.size())) return "" + ((Tick) ticks.get(group)).numTickers(); return ""; } int group = -1; int client = -1; int clistart = which.indexOf("-"); if ((grpstart >= 0) && (clistart > grpstart)) { group = CMath.s_int(which.substring(grpstart, clistart)); client = CMath.s_int(which.substring(clistart + 1)); } if ((group < 0) || (client < 0) || (group >= ticks.size())) return ""; Tick almostTock = (Tick) ticks.get(group); if (client >= almostTock.numTickers()) return ""; TockClient C = almostTock.fetchTickerByIndex(client); if (C == null) return ""; if (which.toLowerCase().startsWith("tickername")) { Tickable E = C.clientObject; if ((E instanceof Ability) && (E.ID().equals("ItemRejuv"))) E = ((Ability) E).affecting(); if (E instanceof Room) return CMLib.map().getExtendedRoomID((Room) E); if (E != null) return E.name(); return "!NULL!"; } else if (which.toLowerCase().startsWith("tickerid")) return "" + C.tickID; else if (which.toLowerCase().startsWith("tickerstatus")) return ((C.clientObject == null) ? "" : ("" + C.clientObject.getTickStatus())); else if (which.toLowerCase().startsWith("tickercodeword")) return getTickStatusSummary(C.clientObject); else if (which.toLowerCase().startsWith("tickertickdown")) return "" + C.tickDown; else if (which.toLowerCase().startsWith("tickerretickdown")) return "" + C.reTickDown; else if (which.toLowerCase().startsWith("tickermillitotal")) return "" + C.milliTotal; else if (which.toLowerCase().startsWith("tickermilliavg")) { if (C.tickTotal == 0) return "0"; return "" + (C.milliTotal / C.tickTotal); } else if (which.toLowerCase().startsWith("tickerlaststartmillis")) return "" + C.lastStart; else if (which.toLowerCase().startsWith("tickerlaststopmillis")) return "" + C.lastStop; else if (which.toLowerCase().startsWith("tickerlaststartdate")) return CMLib.time().date2String(C.lastStart); else if (which.toLowerCase().startsWith("tickerlaststopdate")) return CMLib.time().date2String(C.lastStop); else if (which.toLowerCase().startsWith("tickerlastduration")) { if (C.lastStop >= C.lastStart) return CMLib.english().returnTime(C.lastStop - C.lastStart, 0); return CMLib.english().returnTime(System.currentTimeMillis() - C.lastStart, 0); } else if (which.toLowerCase().startsWith("tickersuspended")) return "" + C.suspended; return ""; }
/** * Method used to determine if parameter String is numeric * * @param str examined string * @return true when input String is numeric, false when input String is not numeric */ private boolean stringNumericCheck(String str) { for (char c : str.toCharArray()) { if (!Character.isDigit(c)) { return false; } } return true; }
/** * Checks whether a string contains a digit. Used for ignoring words with numbers * * @param s Word to be checked. * @return True if there is a digit inside the word. */ static boolean containsNoDigit(final String s) { for (int k = 0; k < s.length(); k++) { if (Character.isDigit(s.charAt(k))) { return false; } } return true; }
/** * 判断是否是数字 * * @param str * @return */ public static boolean isNumeric(String str) { for (int i = str.length(); --i >= 0; ) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; }
public int findEndOfOperand() { int i; for (i = index; i < line.length(); i++) { char c = line.charAt(i); if (!Character.isDigit(c)) return i; } return i; }
/** * Gets the next token from a tokenizer and converts it to a double. * * @return The next token in the stream, as a double. * @throws TextParseException The input was invalid or not a double. * @throws IOException An I/O error occurred. */ public double getDouble() throws IOException { String next = getIdentifier(); if (!Character.isDigit(next.charAt(0))) throw exception("expecting an integer"); try { return Double.parseDouble(next); } catch (NumberFormatException e) { throw exception("expecting an floating point value"); } }
/** * Gets the next token from a tokenizer and converts it to a long. * * @return The next token in the stream, as a long. * @throws TextParseException The input was invalid or not a long. * @throws IOException An I/O error occurred. */ public long getLong() throws IOException { String next = getIdentifier(); if (!Character.isDigit(next.charAt(0))) throw exception("expecting an integer"); try { return Long.parseLong(next); } catch (NumberFormatException e) { throw exception("expecting an integer"); } }
public boolean isDigit(int keyCode) { if (Character.isDigit(keyCode) || keyCode == SWT.DEL || keyCode == 8 // ascii of back space || keyCode == SWT.ARROW_LEFT || keyCode == SWT.ARROW_RIGHT) { return true; } return false; }
// check the lexim type Type getType(String _lexim) { if (_lexim == null || _lexim.length() == 0) { // checking the _lexim to see if we reached the end of file or and empty one System.out.println("EoF"); return null; // System.exit(0); } if (Character.isAlphabetic( _lexim.charAt( 0))) // if the first char is alphabetic then chech to see if the token is a keyword if // not return type is ID { for (String keyword1 : keyword) { if (_lexim.equals(keyword1)) { return Type.keyword; } } return Type.id; } else if (Character.isDigit(_lexim.charAt(0)) || _lexim.charAt(0) == '-') { // check if it is a number for (int i = 1; i < _lexim.length(); i++) { if (!Character.isDigit(_lexim.charAt(i))) return Type.unkown; } return Type.number; } else if (!Character.isAlphabetic(_lexim.charAt(0)) && !Character.isDigit(_lexim.charAt(0))) { // checking the other casese like the =,+ ... for (String tempkeyword : keyword) { if (_lexim.equals(tempkeyword)) { return Type.keyword; } } if (_lexim.charAt(0) == '\'' && _lexim.charAt(2) == '\'') { // checking the value of a char var type return Type.value; } } return Type.unkown; }
public int myAtoi(String str) { if (str.length() == 0) return 0; int start_ind = 0, end_ind = 0; boolean start = false, f_blank = false; String s = str.trim(); if (s.length() == 0) return 0; char f = s.charAt(0); if (f != '+' && f != '-' && !Character.isDigit(f)) { return 0; } // System.out.println(s); for (int i = 0; i <= s.length(); i++) { if (start == true && i == s.length()) { end_ind = i; break; } char c = 'd'; if (i < s.length()) c = s.charAt(i); if ((c == '+' || c == '-' || Character.isDigit(c)) && (start == false)) { start = true; start_ind = i; continue; } if (start == true) { if (!Character.isDigit(c)) { end_ind = i; break; } } } if (start == false) return 0; System.out.println("Indics: " + start_ind + " " + end_ind); String sub = s.substring(start_ind, end_ind); if (sub.equals("+") || sub.equals("-")) return 0; Double d = new Double("2.0"); double r = d.parseDouble(sub); if (r > Integer.MAX_VALUE) return Integer.MAX_VALUE; if (r < Integer.MIN_VALUE) return Integer.MIN_VALUE; int out = Integer.parseInt(sub); System.out.println(out); return out; }
@Override public void setParms(String newParm) { super.setParms(newParm); rates.clear(); cut = 0.05; spaceMaxCut = 0.0; spaceMaxDistance = SpaceObject.Distance.GalaxyRadius.dm; newParm = newParm.toUpperCase(); int x = newParm.indexOf('='); while (x > 0) { int lastSp = newParm.lastIndexOf(' ', x); if (lastSp < 0) lastSp = 0; if ((lastSp >= 0) && (lastSp < x - 1) && (Character.isLetter(newParm.charAt(x - 1)))) { String parm = newParm.substring(lastSp, x).trim().toUpperCase(); while ((x < newParm.length()) && (newParm.charAt(x) != '=')) x++; if (x < newParm.length()) { while ((x < newParm.length()) && (!Character.isDigit(newParm.charAt(x))) && (newParm.charAt(x) != '.')) x++; if (x < newParm.length()) { newParm = newParm.substring(x); x = 0; while ((x < newParm.length()) && ((Character.isDigit(newParm.charAt(x))) || (newParm.charAt(x) == '.'))) x++; double val = CMath.s_double(newParm.substring(0, x)); if (newParm.substring(0, x).indexOf('.') < 0) val = CMath.s_long(newParm.substring(0, x)); if (x < newParm.length()) newParm = newParm.substring(x + 1); else newParm = ""; if (parm.equalsIgnoreCase("default")) parm = ""; if (parm.equalsIgnoreCase("spacemaxcut")) spaceMaxCut = val / 100.0; else if (parm.equalsIgnoreCase("spacemaxdistance")) spaceMaxDistance = Math.round(CMath.mul(SpaceObject.Distance.GalaxyRadius.dm, val / 100.0)); else if (parm.equalsIgnoreCase("cut")) cut = val / 100.0; else rates.put(parm, Double.valueOf(val / 100.0)); } } } x = newParm.indexOf('='); } }