List of usage examples for java.lang Character isDigit
public static boolean isDigit(int codePoint)
From source file:net.metanotion.json.JsonPath.java
private static Lexeme lexToken(final String cs, final int[] position) { final int c = skipWhitespace(cs, position); switch (c) {/*from w ww .j av a 2 s . c o m*/ case -1: return EOF; case '[': return L_BRACE; case ']': return R_BRACE; case '{': return L_CURLY; case '}': return R_CURLY; case '*': return STAR; case '.': return DOT; case '\'': return lexString(cs, position); default: if (Character.isDigit(c) || (c == (int) '-')) { // Lex integer final StringBuilder sb = new StringBuilder(new String(Character.toChars(c))); int digits = Character.isDigit(c) ? 1 : 0; while (true) { final int i = read(cs, position); if (Character.isDigit(i)) { digits++; sb.append(Character.toChars(i)); } else { position[0]--; if (digits == 0) { throw new IllegalArgumentException("Expected at least one digit [0-9]"); } return new Lexeme(Token.INT, Long.valueOf(sb.toString())); } } } else { // Lex identifier position[0]--; final StringBuilder sb = new StringBuilder(); while (true) { final int i = read(cs, position); if (i == -1) { return new Lexeme(Token.ID, sb.toString()); } if (Character.isLetter(i) || (i == (int) '_') || Character.isDigit(i)) { sb.append(Character.toChars(i)); } else { position[0]--; return new Lexeme(Token.ID, sb.toString()); } } } } }
From source file:com.flexive.shared.FxFormatUtils.java
/** * Returns true if the char is a valid RGB value. * * @param ch the character to check/*from w ww . j av a 2s . c o m*/ * @return true if the char is a valid RGB value */ private static boolean isValidRGBChar(final char ch) { return (Character.isDigit(ch) || ch == 'A' || ch == 'B' || ch == 'C' || ch == 'D' || ch == 'E' || ch == 'F'); }
From source file:org.impotch.calcul.impot.cantonal.ge.pp.ChargeurFichierEconometre.java
/**************************************************/ public Object[][] charger(int periode, boolean famille) throws IOException { int anneeCouranteMoinsUn = Calendar.getInstance().get(Calendar.YEAR) - 1; if (periode < 1995 || periode > anneeCouranteMoinsUn) { logger.error("La priode fiscale " + periode + " n'est pas dfinie !"); throw new IllegalArgumentException("La priode fiscale " + periode + " n'est pas dfinie !"); }/* w w w . j a va 2s . co m*/ int indexImpot = 2 * (periode - 1995) + (famille ? 1 : 0) + 1; BufferedReader reader = new BufferedReader(new InputStreamReader(fichier.getInputStream())); int numLigne = 1; String ligne = reader.readLine(); List<Point> points = new LinkedList<Point>(); while (null != ligne) { try { String[] tokens = ligne.split(";"); String revenu = tokens[0].trim(); revenu = revenu.substring(0, revenu.length() - 1); if (Character.isDigit(revenu.charAt(0))) { points.add(new Point(revenu, tokens[indexImpot].trim())); } } catch (ParseException pe) { throw new TypeMismatchDataAccessException( "Erreur de lecture dans la ressource " + fichier.getFilename() + " la ligne " + numLigne, pe); } catch (NumberFormatException nfe) { throw new TypeMismatchDataAccessException( "Erreur de lecture dans la ressource " + fichier.getFilename() + " la ligne " + numLigne, nfe); } ligne = reader.readLine(); numLigne++; } reader.close(); Object[][] retour = new Object[points.size()][2]; int i = 0; for (Point pt : points) { retour[i][0] = pt.revenu; retour[i++][1] = pt.impot; } return retour; }
From source file:com.amazonaws.services.iot.demo.danbo.rpi.PiezoBuzzer.java
public String[] play(String songData) throws Exception { ArrayList<Integer> pitches = new ArrayList<Integer>(); ArrayList<Float> durations = new ArrayList<Float>(); int default_dur = 4; int default_oct = 6; int bpm = 63; int num;//from ww w .ja v a 2s .c om long wholenote; long duration; int note; int scale; int p = 0; while (songData.charAt(p) != ':') { p++; } p++; if (songData.charAt(p) == 'd') { p++; p++; num = 0; while (Character.isDigit(songData.charAt(p))) { num = (num * 10) + Integer.valueOf(songData.charAt(p++) - '0'); } if (num > 0) { default_dur = num; } p++; } if (songData.charAt(p) == 'o') { p++; p++; num = Integer.valueOf(songData.charAt(p++) - '0'); if (num >= 3 && num <= 7) { default_oct = num; } p++; } if (songData.charAt(p) == 'b') { p++; p++; num = 0; while (Character.isDigit(songData.charAt(p))) { num = (num * 10) + Integer.valueOf(songData.charAt(p++) - '0'); } bpm = num; p++; } wholenote = (60 * 1000L / bpm) * 4; while (p < songData.length()) { num = 0; while (Character.isDigit(songData.charAt(p))) { num = (num * 10) + Integer.valueOf(songData.charAt(p++) - '0'); } if (num != 0) { duration = wholenote / num; } else { duration = wholenote / default_dur; } note = 0; switch (songData.charAt(p)) { case 'c': note = 1; break; case 'd': note = 3; break; case 'e': note = 5; break; case 'f': note = 6; break; case 'g': note = 8; break; case 'a': note = 10; break; case 'b': note = 12; break; case 'p': note = 0; default: note = 0; } p++; if (songData.charAt(p) == '#') { note++; p++; } if (songData.charAt(p) == '.') { duration += duration / 2; p++; } if (Character.isDigit(songData.charAt(p))) { scale = Integer.valueOf(songData.charAt(p) - '0'); p++; } else { scale = default_oct; } scale += OCTAVE_OFFSET; if (songData.charAt(p) == ',') { p++; } if (note != 0) { int n = note_pitch[(scale - 4) * 12 + note]; pitches.add(new Integer(n)); durations.add(new Float(duration) / new Float(1000)); } else { pitches.add(new Integer(0)); durations.add(new Float(duration) / new Float(1000)); } } String[] returnVal = new String[2]; returnVal[0] = pitches.toString().substring(1, pitches.toString().length() - 1).replaceAll(" ", ""); returnVal[1] = durations.toString().substring(1, durations.toString().length() - 1).replaceAll(" ", ""); return returnVal; }
From source file:com.github.gilbertotorrezan.viacep.se.ViaCEPClient.java
/** * Executa a consulta de endereo a partir de um CEP. * /*from w w w .j a va2s . c om*/ * @param cep CEP da localidade onde se quer consultar o endereo. Precisa ter 8 dgitos - a formatao feita pelo cliente. * CEPs vlidos (que contm 8 dgitos): "20930-040", "abc0 1311000xy z", "20930 040". CEPs invlidos (que no contm 8 dgitos): "00000", "abc", "123456789" * * @return O endereo encontrado para o CEP, ou <code>null</code> caso no tenha sido encontrado. * @throws IOException em casos de erro de conexo. * @throws IllegalArgumentException para CEPs que no possuam 8 dgitos. */ public ViaCEPEndereco getEndereco(String cep) throws IOException { char[] chars = cep.toCharArray(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < chars.length; i++) { if (Character.isDigit(chars[i])) { builder.append(chars[i]); } } cep = builder.toString(); if (cep.length() != 8) { throw new IllegalArgumentException("CEP invlido - deve conter 8 dgitos: " + cep); } String urlString = getHost() + cep + "/json/"; URL url = new URL(urlString); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); ViaCEPEndereco obj = getService().beanFrom(ViaCEPEndereco.class, in); if (obj == null || obj.getCep() == null) { return null; } return obj; } finally { urlConnection.disconnect(); } }
From source file:gridool.util.system.SystemUtils.java
private static String getJavaVersion(String versionStr) { for (int i = 0; i < versionStr.length(); i++) { char c = versionStr.charAt(i); if (Character.isDigit(c)) { return versionStr.substring(i); }//from w ww .jav a2s . c om } return null; }
From source file:edu.smc.mediacommons.panels.PasswordPanel.java
public PasswordPanel() { setLayout(null);//from w ww.j av a2 s. co m add(Utils.createLabel("Enter a Password to Test", 60, 30, 200, 20, null)); final JButton test = Utils.createButton("Test", 260, 50, 100, 20, null); add(test); final JPasswordField fieldPassword = new JPasswordField(32); fieldPassword.setBounds(60, 50, 200, 20); add(fieldPassword); final PieDataset dataset = createSampleDataset(33, 33, 33); final JFreeChart chart = createChart(dataset); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(300, 300)); chartPanel.setBounds(45, 80, 340, 250); add(chartPanel); test.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String password = new String(fieldPassword.getPassword()); if (password.isEmpty() || password == null) { JOptionPane.showMessageDialog(getParent(), "Warning! The input was blank!", "Invalid Input", JOptionPane.ERROR_MESSAGE); } else { int letterCount = 0, numberCount = 0, specialCount = 0, total = password.length(); for (char c : password.toCharArray()) { if (Character.isLetter(c)) { letterCount++; } else if (Character.isDigit(c)) { numberCount++; } else { specialCount++; } } long totalCombinations = 0; double percentLetters = 0; double percentNumbers = 0; double percentCharacters = 0; if (letterCount > 0) { totalCombinations += (factorial(26) / factorial(26 - letterCount)); percentLetters = (letterCount + 0.0 / total); } if (numberCount > 0) { totalCombinations += (factorial(10) / factorial(10 - numberCount)); percentNumbers = (numberCount + 0.0 / total); } if (specialCount > 0) { totalCombinations += (factorial(40) / factorial(40 - specialCount)); percentCharacters = (specialCount + 0.0 / total); } PieDataset dataset = createSampleDataset(percentLetters, percentNumbers, percentCharacters); JFreeChart chart = createChart(dataset); chartPanel.setChart(chart); JOptionPane.showMessageDialog(getParent(), "Total Combinations: " + totalCombinations + "\nAssuming Rate Limited, Single: " + (totalCombinations / 1000) + " seconds" + "\n\nBreakdown:\nLetters: " + percentLetters + "%\nNumbers: " + percentNumbers + "%\nCharacters: " + percentCharacters + "%"); } } }); setVisible(true); }
From source file:com.slimsmart.common.util.code.EncodeUtil.java
public static String escape(String src) { int i;/* ww w .j av a 2s. c o m*/ char j; StringBuffer tmp = new StringBuffer(); tmp.ensureCapacity(src.length() * 6); for (i = 0; i < src.length(); i++) { j = src.charAt(i); if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j)) tmp.append(j); else if (j < 256) { tmp.append("%"); if (j < 16) tmp.append("0"); tmp.append(Integer.toString(j, 16)); } else { tmp.append("%u"); tmp.append(Integer.toString(j, 16)); } } return tmp.toString(); }
From source file:NaturalOrderComparator.java
public int compare(Object o1, Object o2) { String a = o1.toString();//from www. java2 s . c o m String b = o2.toString(); int ia = 0, ib = 0; int nza = 0, nzb = 0; char ca, cb; int result; while (true) { // only count the number of zeroes leading the last number compared nza = nzb = 0; ca = charAt(a, ia); cb = charAt(b, ib); // skip over leading spaces or zeros while (Character.isSpaceChar(ca) || ca == '0') { if (ca == '0') { nza++; } else { // only count consecutive zeroes nza = 0; } ca = charAt(a, ++ia); } while (Character.isSpaceChar(cb) || cb == '0') { if (cb == '0') { nzb++; } else { // only count consecutive zeroes nzb = 0; } cb = charAt(b, ++ib); } // process run of digits if (Character.isDigit(ca) && Character.isDigit(cb)) { if ((result = compareRight(a.substring(ia), b.substring(ib))) != 0) { return result; } } if (ca == 0 && cb == 0) { // The strings compare the same. Perhaps the caller // will want to call strcmp to break the tie. return nza - nzb; } if (ca < cb) { return -1; } else if (ca > cb) { return +1; } ++ia; ++ib; } }
From source file:com.sjsu.crawler.util.RandomStringUtilsTest.java
/** * Test the implementation//from w ww .j a v a 2s . com */ @Test public void testRandomStringUtils() { String r1 = RandomStringUtils.random(50); assertEquals("random(50) length", 50, r1.length()); String r2 = RandomStringUtils.random(50); assertEquals("random(50) length", 50, r2.length()); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.randomAscii(50); assertEquals("randomAscii(50) length", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("char between 32 and 127", r1.charAt(i) >= 32 && r1.charAt(i) <= 127); } r2 = RandomStringUtils.randomAscii(50); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.randomAlphabetic(50); assertEquals("randomAlphabetic(50)", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("r1 contains alphabetic", Character.isLetter(r1.charAt(i)) && !Character.isDigit(r1.charAt(i))); } r2 = RandomStringUtils.randomAlphabetic(50); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.randomAlphanumeric(50); assertEquals("randomAlphanumeric(50)", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("r1 contains alphanumeric", Character.isLetterOrDigit(r1.charAt(i))); } r2 = RandomStringUtils.randomAlphabetic(50); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.randomNumeric(50); assertEquals("randomNumeric(50)", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("r1 contains numeric", Character.isDigit(r1.charAt(i)) && !Character.isLetter(r1.charAt(i))); } r2 = RandomStringUtils.randomNumeric(50); assertTrue("!r1.equals(r2)", !r1.equals(r2)); String set = "abcdefg"; r1 = RandomStringUtils.random(50, set); assertEquals("random(50, \"abcdefg\")", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("random char in set", set.indexOf(r1.charAt(i)) > -1); } r2 = RandomStringUtils.random(50, set); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.random(50, (String) null); assertEquals("random(50) length", 50, r1.length()); r2 = RandomStringUtils.random(50, (String) null); assertEquals("random(50) length", 50, r2.length()); assertTrue("!r1.equals(r2)", !r1.equals(r2)); set = "stuvwxyz"; r1 = RandomStringUtils.random(50, set.toCharArray()); assertEquals("random(50, \"stuvwxyz\")", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("random char in set", set.indexOf(r1.charAt(i)) > -1); } r2 = RandomStringUtils.random(50, set); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.random(50, (char[]) null); assertEquals("random(50) length", 50, r1.length()); r2 = RandomStringUtils.random(50, (char[]) null); assertEquals("random(50) length", 50, r2.length()); assertTrue("!r1.equals(r2)", !r1.equals(r2)); final long seed = System.currentTimeMillis(); r1 = RandomStringUtils.random(50, 0, 0, true, true, null, new Random(seed)); r2 = RandomStringUtils.random(50, 0, 0, true, true, null, new Random(seed)); assertEquals("r1.equals(r2)", r1, r2); r1 = RandomStringUtils.random(0); assertEquals("random(0).equals(\"\")", "", r1); }