List of usage examples for java.util Scanner next
public String next()
From source file:opennlp.tools.doc_classifier.DocClassifier.java
public static String formClassifQuery(String pageContentReader, int maxRes) { // We want to control which delimiters we substitute. For example '_' & // \n we retain pageContentReader = pageContentReader.replaceAll("[^A-Za-z0-9 _\\n]", ""); Scanner in = new Scanner(pageContentReader); in.useDelimiter("\\s+"); Map<String, Integer> words = new HashMap<String, Integer>(); while (in.hasNext()) { String word = in.next(); if (!StringUtils.isAlpha(word) || word.length() < 4) continue; if (!words.containsKey(word)) { words.put(word, 1);//w ww . j a v a2 s . c om } else { words.put(word, words.get(word) + 1); } } in.close(); words = ValueSortMap.sortMapByValue(words, false); List<String> resultsAll = new ArrayList<String>(words.keySet()), results = null; int len = resultsAll.size(); if (len > maxRes) results = resultsAll.subList(len - maxRes, len - 1); // get maxRes // elements else results = resultsAll; return results.toString().replaceAll("(\\[|\\]|,)", " ").trim(); }
From source file:com.pieframework.model.repository.ModelStore.java
protected static String parse(String query) { ComponentQueryBuilder builder = new ComponentQueryBuilder(); Scanner scanner = new Scanner(query); scanner.useDelimiter(PATH_DELIMITER); while (scanner.hasNext()) { final String[] nameValue = scanner.next().split(NAME_VALUE_DELIMITER); if (nameValue.length == 0 || nameValue.length > 2) { throw new IllegalArgumentException("Illegal format"); }//from w w w . j a va2s. c o m builder.add(nameValue[0]); } return builder.build(); }
From source file:com.ibm.sbt.sample.web.util.SnippetFactory.java
private static String httpGet(String url) { try {/* w w w . j av a2 s . c o m*/ HttpClient httpClient = SSLUtil.wrapHttpClient(new DefaultHttpClient()); HttpGet request = new HttpGet(url); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity content = response.getEntity(); java.util.Scanner scanner = new java.util.Scanner(content.getContent()); scanner.useDelimiter("\\A"); String result = scanner.hasNext() ? scanner.next() : ""; scanner.close(); return result; } else { return null; } } catch (Exception e) { return null; } }
From source file:org.apache.airavata.registry.tool.DBMigrator.java
public static void updateDB(String jdbcUrl, String jdbcUser, String jdbcPwd) { relativePath = "db-scripts/" + getIncrementedVersion(currentAiravataVersion) + "/"; InputStream sqlStream = null; Scanner in = new Scanner(System.in); if (jdbcUrl == null || jdbcUrl.equals("")) { System.out.println("Enter JDBC URL : "); jdbcUrl = in.next(); }//from w ww.j av a 2s.c om if (jdbcUser == null || jdbcUser.equals("")) { System.out.println("Enter JDBC Username : "); jdbcUser = in.next(); } if (jdbcPwd == null || jdbcPwd.equals("")) { System.out.println("Enter JDBC password : "); jdbcPwd = in.next(); } String dbType = getDBType(jdbcUrl); String jdbcDriver = null; Connection connection; try { File file = null; if (dbType.contains("derby")) { jdbcDriver = "org.apache.derby.jdbc.ClientDriver"; sqlStream = DBMigrator.class.getClassLoader().getResourceAsStream(relativePath + MIGRATE_SQL_DERBY); } else if (dbType.contains("mysql")) { jdbcDriver = "com.mysql.jdbc.Driver"; sqlStream = DBMigrator.class.getClassLoader().getResourceAsStream(relativePath + MIGRATE_SQL_MYSQL); } Class.forName(jdbcDriver).newInstance(); connection = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPwd); if (canUpdated(connection)) { executeSQLScript(connection, sqlStream); //update configuration table with airavata version updateConfigTable(connection); } } catch (ClassNotFoundException e) { e.printStackTrace(); logger.error("Unable to find SQL scripts...", e); } catch (InstantiationException e) { e.printStackTrace(); logger.error("Error while updating the database...", e); } catch (IllegalAccessException e) { e.printStackTrace(); logger.error("Error while updating the database...", e); } catch (SQLException e) { e.printStackTrace(); logger.error("Error while updating the database...", e); } catch (Exception e) { e.printStackTrace(); logger.error("Error while updating the database...", e); } }
From source file:info.varden.anatychia.Main.java
public static String getTextFromJarPath(String path) { try {/*from w w w . java 2 s . c o m*/ InputStream input = Main.class.getResourceAsStream(path); Scanner s = new Scanner(input, "UTF-8").useDelimiter("\\A"); String str = s.hasNext() ? s.next() : "Error: Failed to read file " + path; s.close(); input.close(); return str; } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); return "Error: Failed to read file " + path; } }
From source file:no.imr.stox.functions.utils.RUtils.java
public static String callR(String rFolder, String fileName, String cmd) { try {/*from www. j av a 2s . co m*/ String triggerFile = getTmpDir() + fileName; try (PrintWriter pw = new PrintWriter(triggerFile)) { pw.println(cmd); } Process proc = callR(rFolder, triggerFile, false); if (proc != null) { java.io.InputStream is = proc.getInputStream(); java.util.Scanner s = new java.util.Scanner(is).useDelimiter(";"); while (s.hasNext()) { String str = s.next(); return StringUtils.substringBetween(str, "\"", "\""); } } } catch (IOException ex) { } return ""; }
From source file:wordGame.Util.WebUtil.java
public static Board GetBoardFromServer(String boardID) { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet getMethod = new HttpGet(WEBGETBOARD + boardID); Scanner fileScanner = null; String boardString = null;/*from www .ja v a 2 s . co m*/ try { HttpResponse response = httpclient.execute(getMethod); HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { fileScanner = new Scanner(new BufferedReader(new InputStreamReader(responseEntity.getContent()))); while (fileScanner.hasNext()) { boardString = fileScanner.next(); return BoardGenerator.generateCubesFromWebString(boardString); } } } catch (IOException e) { return null; } return null; }
From source file:utils.ImportExportUtils.java
/** * setting SSL Cert/* w w w . jav a 2 s . c om*/ */ static void setSSlCert() { ApiImportExportConfiguration config = ApiImportExportConfiguration.getInstance(); String trustStore; Scanner sc = new Scanner(System.in, ImportExportConstants.CHARSET); if (StringUtils.isBlank(config.getTrustStoreUrl())) { System.out.print("Enter the trust store url : "); trustStore = sc.next(); } else { trustStore = config.getTrustStoreUrl(); } System.out.print("Enter trust store password : "); char[] trustStorePassword = sc.next().toCharArray(); if (StringUtils.isNotBlank(trustStore) && StringUtils.isNotBlank(String.valueOf(trustStorePassword))) { System.setProperty(ImportExportConstants.SSL_TRUSTSTORE, trustStore); System.setProperty(ImportExportConstants.SSL_PASSWORD, String.valueOf(trustStorePassword)); } }
From source file:de.ingrid.interfaces.csw.tools.FileUtils.java
/** * Convert stream to string./* w w w . ja v a 2 s . c om*/ * * @param is * @return */ public static String convertStreamToString(java.io.InputStream is) { Scanner scanner = null; try { scanner = new Scanner(is); scanner.useDelimiter("\\A"); String content = scanner.next(); scanner.close(); return content; } catch (java.util.NoSuchElementException e) { return ""; } finally { if (scanner != null) { scanner.close(); } } }
From source file:org.apache.felix.http.itest.BaseIntegrationTest.java
protected static String slurpAsString(InputStream is) throws IOException { // See <weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html> Scanner scanner = new Scanner(is, "UTF-8"); try {//from w w w . j a va2 s. co m scanner.useDelimiter("\\A"); return scanner.hasNext() ? scanner.next() : null; } finally { try { scanner.close(); } catch (Exception e) { // Ignore... } } }