List of usage examples for java.util Scanner next
public String next()
From source file:org.omg.bpmn.miwg.testresult.TestResults.java
public static String convertStreamToString(java.io.InputStream is) { java.util.Scanner s = null; try {//from www .j a v a 2 s . co m s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } finally { s.close(); } }
From source file:UtilFunctions.java
public static String[] parseFileTypes(String inputString) { //ArrayList to store each parsed filetype ArrayList<String> fileTypes = new ArrayList<>(); //Scanner to iterate over the string filetype by filetype Scanner in = new Scanner(inputString); in.useDelimiter("[,] | [, ]"); //Iterate over the string while (in.hasNext()) { fileTypes.add(in.next()); }/* w w w.j a va 2 s .c om*/ if (fileTypes.size() > 0) { //Generate an array from the ArrayList String[] returnArray = new String[fileTypes.size()]; returnArray = fileTypes.toArray(returnArray); //Return that magnificent array return returnArray; } else { return null; } }
From source file:Main.java
/** * Adds all parameters within the Scanner to the list of <code>parameters</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./*from ww w . j av a2 s . c o m*/ * * @param parameters List to add parameters to. * @param scanner Input that contains the parameters to parse. */ public static void parse(final List<NameValuePair> parameters, final Scanner scanner) { scanner.useDelimiter(PARAMETER_SEPARATOR); while (scanner.hasNext()) { String name = null; String value = null; String token = scanner.next(); int i = token.indexOf(NAME_VALUE_SEPARATOR); if (i != -1) { name = token.substring(0, i).trim(); value = token.substring(i + 1).trim(); } else { name = token.trim(); } parameters.add(new BasicNameValuePair(name, value)); } }
From source file:edu.msu.cme.rdp.unifrac.Unifrac.java
private static Map<String, UnifracSample> readSampleMap(String sampleFile) throws IOException { Map<String, UnifracSample> ret = new HashMap(); Map<String, MCSample> sampleMap = new HashMap(); int lineno = 1; Scanner s = new Scanner(new File(sampleFile)).useDelimiter("\n"); while (s.hasNext()) { String line = s.next().trim(); if (line.equals("")) { continue; }// ww w . j a va2s. com String[] tokens = line.split("\\s+"); if (tokens.length < 2) { throw new IOException("Failed to parse sample mapping file (lineno=" + lineno + ")"); } String sampleName = tokens[1]; String seqName = tokens[0]; int sampleCount = 1; try { sampleCount = Integer.parseInt(tokens[2]); } catch (Exception e) { } if (!sampleMap.containsKey(sampleName)) sampleMap.put(sampleName, new MCSample(sampleName)); UnifracSample unifracSample = new UnifracSample(); unifracSample.sample = sampleMap.get(sampleName); unifracSample.count = sampleCount; ret.put(seqName, unifracSample); lineno++; } s.close(); return ret; }
From source file:SerialPortHandler.java
public static String convertStreamToStringIn(java.io.InputStream is) { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); System.out.println("Made it here!!"); return s.hasNext() ? s.next() : ""; }
From source file:com.cloudant.client.org.lightcouch.internal.CouchDbUtil.java
public static String streamToString(InputStream in) { Scanner s = new Scanner(in, "UTF-8"); s.useDelimiter("\\A"); String str = s.hasNext() ? s.next() : null; close(in);/*from w ww . j a v a 2 s . co m*/ s.close();// mdb return str; }
From source file:org.my.eaptest.UploadServlet.java
private static String fromStream(InputStream is) { Scanner s = new Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
From source file:com.alibaba.openapi.client.util.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.//from www. ja va 2s . co 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:cz.autoclient.github.json.GitHubJson.java
public static JSONObject fromURL(URL url) { Scanner s; try {//from w ww .jav a 2s . c o m s = new Scanner(url.openStream(), "UTF-8"); } catch (IOException ex) { throw new IllegalArgumentException("JSON file not found at " + url); } s.useDelimiter("\\A"); if (s.hasNext()) { String out = s.next(); try { return new JSONObject(out); } catch (JSONException ex) { throw new IllegalArgumentException( "Invalid JSON contents at " + url + " - \n JSON error:" + ex); //Logger.getLogger(DataLoader.class.getName()).log(Level.SEVERE, null, ex); } } else throw new IllegalArgumentException("JSON file not found at " + url); }
From source file:apiPull.getItem.java
public static ArrayList numberOfIds() { // go to gw2 https://api.guildwars2.com/v2/items and get a list of items // return the arraylist, use to loop through implimentations ArrayList ids = new ArrayList(); // Make a URL to the web page URL url = null;//from ww w . jav a2 s . co m try { url = new URL("https://api.guildwars2.com/v2/items"); } catch (MalformedURLException ex) { Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex); } // Get the input stream through URL Connection URLConnection con = null; try { con = url.openConnection(); } catch (IOException ex) { Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex); } InputStream is = null; try { is = con.getInputStream(); // Once you have the Input Stream, it's just plain old Java IO stuff. // For this case, since you are interested in getting plain-text web page // I'll use a reader and output the text content to System.out. // For binary content, it's better to directly read the bytes from stream and write // to the target file. } catch (IOException ex) { Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex); } BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = null; try { // read each line while ((line = br.readLine()) != null) { Scanner scanner = new Scanner(line); scanner.useDelimiter(","); while (scanner.hasNext()) { ids.add(scanner.next()); } scanner.close(); } } catch (IOException ex) { Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex); } return ids; }