List of usage examples for java.util Scanner next
public String next()
From source file:ReversePolishNotation.java
/** * This method will calculate the answer of the given Reverse Polar Notation * equation. All exceptions are thrown to the parent for handling. * //from w w w . j a v a 2 s . com * @param input * is the equation entered by the user * @throws EmptyRPNException * when there is no RPN equation to be evaluated. * @throws RPNDivideByZeroException * when the RPN equation attempts to divide by zero. * @throws RPNUnderflowException * when the RPN equation has a mathematical operator before * there are enough numerical values for it to evaluate. * @throws InvalidRPNException * when the RPN equation is a String which is unable to be * manipulated. * @throws RPNOverflowException * when the RPN equation has too many numerical values and not * enough mathematical operators with which to evaluate them. * @return the top item of the stack; the calculated answer of the Reverse * Polish Notation equation */ public static double calcRPN(String input) { // eliminate any leading or trailing whitespace from input input = input.trim(); // scanner to manipulate input and stack to store double values String next; Stack<Double> stack = new Stack<Double>(); Scanner scan = new Scanner(input); // loop while there are tokens left in scan while (scan.hasNext()) { // retrieve the next token from the input next = scan.next(); // see if token is mathematical operator if (nextIsOperator(next)) { // ensure there are enough numbers on stack if (stack.size() > 1) { if (next.equals("+")) { stack.push((Double) stack.pop() + (Double) stack.pop()); } else if (next.equals("-")) { stack.push(-(Double) stack.pop() + (Double) stack.pop()); } else if (next.equals("*")) { stack.push((Double) stack.pop() * (Double) stack.pop()); } else if (next.equals("/")) { double first = stack.pop(); double second = stack.pop(); if (first == 0) { System.out.println("The RPN equation attempted to divide by zero."); } else { stack.push(second / first); } } } else { System.out.println( "A mathematical operator occured before there were enough numerical values for it to evaluate."); } } else { try { stack.push(Double.parseDouble(next)); } catch (NumberFormatException c) { System.out.println("The string is not a valid RPN equation."); } } } if (stack.size() > 1) { System.out.println( "There too many numbers and not enough mathematical operators with which to evaluate them."); } return (Double) stack.pop(); }
From source file:com.almende.pi5.lch.SimManager.java
private static String convertStreamToString(InputStream is) { final Scanner s = new Scanner(is, "UTF-8"); s.useDelimiter("\\A"); final String result = s.hasNext() ? s.next() : ""; s.close();//from w w w . j a v a2 s. c om return result; }
From source file:com.sample.ecommerce.util.ElasticSearchUtil.java
public static String getContentFromClasspath(String resourcePath) { InputStream inputStream = ElasticSearchUtil.class.getResourceAsStream(resourcePath); java.util.Scanner scanner = new java.util.Scanner(inputStream, "UTF-8").useDelimiter("\\A"); String theString = scanner.hasNext() ? scanner.next() : ""; scanner.close();//from ww w . ja va2s. c o m return theString; }
From source file:com.freemedforms.openreact.db.DbSchema.java
/** * Attempt to run a database patch./*from w w w . ja v a2 s .co m*/ * * @param patchFilename * @return Success. * @throws SQLException */ public static boolean applyPatch(String patchFilename) throws SQLException { Connection c = Configuration.getConnection(); String patch = null; Scanner scanner; try { scanner = new Scanner(new File(patchFilename)).useDelimiter("\\Z"); patch = scanner.next(); scanner.close(); } catch (FileNotFoundException ex) { log.error(ex); return false; } Statement cStmt = c.createStatement(); boolean status = false; try { log.debug("Using patch length = " + patch.length()); cStmt.execute(patch); // cStmt = c.prepareStatement(patch); // cStmt.execute(); log.info("Patch succeeded"); status = true; } catch (NullPointerException npe) { log.error("Caught NullPointerException", npe); } catch (Throwable e) { log.error(e.toString()); } finally { DbUtil.closeSafely(cStmt); DbUtil.closeSafely(c); } return status; }
From source file:nz.org.winters.appspot.acrareporter.server.MappingUploadHandler.java
public static String convertStreamToString(java.io.InputStream is) { java.util.Scanner s = new java.util.Scanner(is); s.useDelimiter("\\A"); try {//from www . jav a2 s. co m return s.hasNext() ? s.next() : ""; } finally { s.close(); } }
From source file:net.firejack.platform.core.utils.StringUtils.java
/** * @param value// w ww.j ava 2 s .com * @param replacement * @return */ public static String changeWhiteSpacesWithSymbol(String value, String replacement) { if (StringUtils.isBlank(value)) { return ""; } if (replacement == null) { throw new IllegalArgumentException("Could not process value using replacement which is null."); } StringBuilder sb = new StringBuilder(); Scanner sc = new Scanner(value); sb.append(sc.next()); while (sc.hasNext()) { sb.append(replacement).append(sc.next()); } return sb.toString(); }
From source file:com.yoavst.quickapps.URLEncodedUtils.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 NameValuePair NameValuePairs} a=1, b=2, and c=3 to the * list of parameters./* ww w .ja v a 2 s . c o m*/ * * @param parameters List to add parameters to. * @param scanner Input that contains the parameters to parse. * @param encoding Encoding to use when decoding the parameters. */ public static void parse(final List<NameValuePair> parameters, final Scanner scanner, final String encoding) { scanner.useDelimiter(PARAMETER_SEPARATOR); while (scanner.hasNext()) { final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR); if (nameValue.length == 0 || nameValue.length > 2) throw new IllegalArgumentException("bad parameter"); final String name = decode(nameValue[0], encoding); String value = null; if (nameValue.length == 2) value = decode(nameValue[1], encoding); parameters.add(new BasicNameValuePair(name, value)); } }
From source file:driimerfinance.helpers.FinanceHelper.java
/** * Checks if the license is valid using the License Key Server * * @param license key to check// w w w . ja v a 2 s . co m * @return validity of the license (true or false) */ public static boolean checkLicense(String licenseKey) { HttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost("http://driimerfinance.michaelkohler.info/checkLicense"); List<NameValuePair> params = new ArrayList<NameValuePair>(2); params.add(new BasicNameValuePair("key", licenseKey)); // pass the key to the server try { httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); } catch (UnsupportedEncodingException e) { return false; } HttpResponse response = null; try { response = httpclient.execute(httppost); // get the response from the server } catch (IOException e) { return false; } HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = null; try { instream = entity.getContent(); @SuppressWarnings("resource") java.util.Scanner s = new java.util.Scanner(instream).useDelimiter("\\A"); // parse the response String responseString = s.hasNext() ? s.next() : ""; instream.close(); return responseString.contains("\"valid\":true"); // if the license is valid, return true, else false } catch (IllegalStateException | IOException e) { return false; } } return false; }
From source file:org.t3.metamediamanager.OmdbProvider.java
/** * Converts an InputStream object into a String object. * @param is/*from ww w. ja v a2 s . c o m*/ * The InputStream you want to convert. * @return * A String Object. */ private static String convertStreamToString(InputStream is) { Scanner s = new Scanner(is); Scanner s2 = s.useDelimiter("\\A"); String res = s.hasNext() ? s.next() : ""; s2.close(); s.close(); return res; }
From source file:org.bibsonomy.rest.utils.HeaderUtils.java
/** * /* www.j a v a 2 s . c o m*/ * @param acceptHeader * the HTML ACCEPT Header * <br/>example: * <code>ACCEPT: text/xml,text/html;q=0.9,text/plain;q=0.8,image/png</code> * would be interpreted in the following precedence: * <ol> * <li>text/xml</li> * <li>image/png</li> * <li>text/html</li> * <li>text/plain</li> * </ol> * ) * @return a sorted map with the precedences */ public static SortedMap<Double, Vector<String>> getPreferredTypes(final String acceptHeader) { // maps the q-value to output format (reverse order) final SortedMap<Double, Vector<String>> preferredTypes = new TreeMap<Double, Vector<String>>( new Comparator<Double>() { @Override public int compare(Double o1, Double o2) { if (o1.doubleValue() > o2.doubleValue()) return -1; else if (o1.doubleValue() < o2.doubleValue()) return 1; else return o1.hashCode() - o2.hashCode(); } }); if (!present(acceptHeader)) { return preferredTypes; } // fill map with q-values and formats final Scanner scanner = new Scanner(acceptHeader.toLowerCase()); scanner.useDelimiter(","); while (scanner.hasNext()) { final String[] types = scanner.next().split(";"); final String type = types[0]; double qValue = 1; if (types.length != 1) { /* * FIXME: we get * java.lang.NumberFormatException: For input string: "screen" * in the error log because the format we assume seems to be * different by some clients. Until we find out, what is really * wrong (our parsing or the client), we are more careful with * parsing external data. */ try { qValue = Double.parseDouble(types[1].split("=")[1]); } catch (NumberFormatException e) { qValue = 0; log.error("Couldn't parse accept header '" + acceptHeader + "'"); } } Vector<String> v = preferredTypes.get(qValue); if (!preferredTypes.containsKey(qValue)) { v = new Vector<String>(); preferredTypes.put(qValue, v); } v.add(type); } return preferredTypes; }