List of usage examples for java.util Scanner hasNext
public boolean hasNext()
From source file:core.PlanC.java
/** * retrive global identificator from <code>wmic</code> * //from w ww .ja va 2s .c o m * @param gid - gobal id * @param vn - variable name * * @return variable value */ private static String getWmicValue(String gid, String vn) { String rval = null; try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(new String[] { "wmic", gid, "get", vn }); InputStream is = process.getInputStream(); Scanner sc = new Scanner(is); while (sc.hasNext()) { String next = sc.next(); if (vn.equals(next)) { rval = sc.next().trim(); break; } } is.close(); } catch (IOException e) { e.printStackTrace(); } return rval; }
From source file:com.mycompany.songbitmaven.RecommendationController.java
public static SongDataSet trackLookup(String text) { Api api = Api.DEFAULT_API;/*w w w.j av a2s .c om*/ final TrackSearchRequest request = api.searchTracks(text).market("US").build(); try { final Page<com.wrapper.spotify.models.Track> trackSearchResult = request.get(); String jsonURL = trackSearchResult.getNext(); URL myurl = null; try { myurl = new URL(jsonURL); } catch (Exception e) { System.out.println("Improper URL " + jsonURL); return null; } // read from the URL Scanner scan = null; try { scan = new Scanner(myurl.openStream()); } catch (Exception e) { System.out.println("Could not connect to " + jsonURL); return null; } String str = new String(); while (scan.hasNext()) { str += scan.nextLine() + "\n"; } scan.close(); Gson gson = new Gson(); System.out.println(jsonURL); SongDataSet dataset = gson.fromJson(str, SongDataSet.class); return dataset; } catch (Exception e) { System.out.println("Something went wrong!" + e.getMessage()); return null; } }
From source file:com.qmetry.qaf.automation.testng.report.ReporterUtil.java
public static String execHostName(String execCommand) { InputStream stream;/*from www . j a v a 2 s . com*/ Scanner s; try { Process proc = Runtime.getRuntime().exec(execCommand); stream = proc.getInputStream(); if (stream != null) { s = new Scanner(stream); s.useDelimiter("\\A"); String val = s.hasNext() ? s.next() : ""; stream.close(); s.close(); return val; } } catch (IOException ioException) { ioException.printStackTrace(); } return ""; }
From source file:com.mcxiaoke.next.http.util.URLUtils.java
/** * Adds all parameters within the Scanner to the list of * <code>parameters</code>, as encoded by <code>encoding</code>. For * example, a scanner containing the string <code>a=1&b=2&c=3</code> would * add the {@link org.apache.http.NameValuePair NameValuePairs} a=1, b=2, and c=3 to the * list of parameters./* w w w.j av a 2 s .c om*/ * * @param parameters List to add parameters to. * @param scanner Input that contains the parameters to parse. * @param parameterSepartorPattern The Pattern string for parameter separators, by convention {@code "[&;]"} * @param charset Encoding to use when decoding the parameters. */ public static void parse(final List<NameValuePair> parameters, final Scanner scanner, final String parameterSepartorPattern, final String charset) { scanner.useDelimiter(parameterSepartorPattern); while (scanner.hasNext()) { String name = null; String value = null; final String token = scanner.next(); final int i = token.indexOf(NAME_VALUE_SEPARATOR); if (i != -1) { name = decodeFormFields(token.substring(0, i).trim(), charset); value = decodeFormFields(token.substring(i + 1).trim(), charset); } else { name = decodeFormFields(token.trim(), charset); } parameters.add(new BasicNameValuePair(name, value)); } }
From source file:net.firejack.platform.core.config.meta.construct.ConfigElementFactory.java
private static boolean allWordsCapitalized(String value) { Scanner sc = new Scanner(value); while (sc.hasNext()) { String word = sc.next();/*from ww w .j a va 2 s. co m*/ if (!StringUtils.capitalize(word).equals(word)) { return false; } } return true; }
From source file:org.apache.solr.ltr.TestRerankBase.java
protected static void buildIndexUsingAdoc(String filepath) throws FileNotFoundException { final Scanner scn = new Scanner(new File(filepath), "UTF-8"); StringBuffer buff = new StringBuffer(); scn.nextLine();//from w w w. ja v a 2s. c o m scn.nextLine(); scn.nextLine(); // Skip the first 3 lines then add everything else final ArrayList<String> docsToAdd = new ArrayList<String>(); while (scn.hasNext()) { String curLine = scn.nextLine(); if (curLine.contains("</doc>")) { buff.append(curLine + "\n"); docsToAdd.add(buff.toString().replace("</add>", "").replace("<doc>", "<add>\n<doc>") .replace("</doc>", "</doc>\n</add>")); if (!scn.hasNext()) { break; } else { curLine = scn.nextLine(); } buff = new StringBuffer(); } buff.append(curLine + "\n"); } for (final String doc : docsToAdd) { assertU(doc.trim()); } assertU(commit()); scn.close(); }
From source file:br.ufrgs.inf.dsmoura.repository.controller.solr.SolrConversionUtil.java
private static List<Term> extractTerms(String fieldName, String value, boolean doubleQuote) { if ((value == null) || (value.trim().length() == 0)) { return new ArrayList<Term>(); }/* ww w . j a va 2 s .c om*/ if (fieldName == null) { throw new RuntimeException("null fieldName"); } if (doubleQuote) { /* Value between double quotes */ List<Term> terms = new ArrayList<Term>(); terms.add(new Term(fieldName, '\"' + value + '\"')); return terms; } else { /* Value between wildcards */ List<Term> terms = new ArrayList<Term>(); Scanner sc = new Scanner(value); while (sc.hasNext()) { terms.add(new Term(fieldName, "*" + sc.next() + "*")); } return terms; } }
From source file:br.ufrgs.inf.dsmoura.repository.controller.solr.SolrConversionUtil.java
public static String fromStringToQuery(String value) { String query = ""; boolean isOpenedQuote = false; Scanner sc = new Scanner(value); while (sc.hasNext()) { String next = sc.next();/*from w ww . ja v a2s .c o m*/ if (next.contains("\"") || isOpenedQuote) { query += next; if (next.contains("\"")) { isOpenedQuote = !isOpenedQuote; } } else { if (next.contains(":")) { query += next.replace(":", ":*") + "*"; } else { query += "*" + next + "*"; } } if (!isOpenedQuote) { query += " OR "; } else { query += " "; } } if (query.endsWith(" OR ")) { return query.substring(0, query.lastIndexOf(" OR ")); } else { return query; } }
From source file:jfractus.app.Main.java
private static void parseSize(String sizeStr, Dimension dim) throws ArgumentParseException, BadValueOfArgumentException { Scanner scanner = new Scanner(sizeStr); scanner.useLocale(Locale.ENGLISH); scanner.useDelimiter("x"); int outWidth = 0, outHeight = 0; try {//www .j av a2s . co m outWidth = scanner.nextInt(); outHeight = scanner.nextInt(); if (outWidth < 0 || outHeight < 0) throw new BadValueOfArgumentException("Bad value of argument"); } catch (InputMismatchException e) { throw new ArgumentParseException("Command line parse exception"); } catch (NoSuchElementException e) { throw new ArgumentParseException("Command line parse exception"); } if (scanner.hasNext()) throw new ArgumentParseException("Command line parse exception"); dim.setSize(outWidth, outHeight); }
From source file:com.dropbox.client2.RESTUtility.java
/** * Reads in content from an {@link HttpResponse} and parses it as a query * string.// w ww . j a v a 2 s. co m * * @param response the {@link HttpResponse}. * * @return a map of parameter names to values from the query string. * * @throws DropboxIOException if any network-related error occurs while * reading in content from the {@link HttpResponse}. * @throws DropboxParseException if a malformed or unknown response was * received from the server. * @throws DropboxException for any other unknown errors. This is also a * superclass of all other Dropbox exceptions, so you may want to * only catch this exception which signals that some kind of error * occurred. */ public static Map<String, String> parseAsQueryString(HttpResponse response) throws DropboxException { HttpEntity entity = response.getEntity(); if (entity == null) { throw new DropboxParseException("Bad response from Dropbox."); } Scanner scanner; try { scanner = new Scanner(entity.getContent()).useDelimiter("&"); } catch (IOException e) { throw new DropboxIOException(e); } Map<String, String> result = new HashMap<String, String>(); while (scanner.hasNext()) { String nameValue = scanner.next(); String[] parts = nameValue.split("="); if (parts.length != 2) { throw new DropboxParseException("Bad query string from Dropbox."); } result.put(parts[0], parts[1]); } return result; }