List of usage examples for java.util Scanner hasNext
public boolean hasNext()
From source file:org.arasthel.almeribus.utils.ReadData.java
public static void leerParadas(Context context) throws IOException, JSONException { InputStream is = context.getAssets().open("paradas.json"); StringBuilder builder = new StringBuilder(); Scanner scan = new Scanner(is); while (scan.hasNext()) { builder.append(scan.nextLine()); }//from w ww . j av a 2s . com scan.close(); is.close(); JSONObject json = new JSONObject(builder.toString()); JSONArray paradas = json.getJSONArray("paradas"); Parada p; JSONObject paradaJson; DataStorage.paradas.clear(); for (int i = 0; i < paradas.length(); i++) { paradaJson = paradas.getJSONObject(i); String coord = paradaJson.optString("coord"); if (coord.length() == 0) { p = new Parada(paradaJson.getInt("id"), paradaJson.getString("nombre")); } else { p = new Parada(paradaJson.getInt("id"), paradaJson.getString("nombre"), paradaJson.getString("coord")); } Log.d("PARADA", p.toString()); DataStorage.paradas.put(p.getId(), p); } }
From source file:org.jboss.fuse.security.role.cxf.Client.java
protected static String inputStreamToString(InputStream is) { Scanner s = new Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
From source file:Main.java
public static double[] readVectorDouble(String path) throws FileNotFoundException { ArrayList<Double> arr = new ArrayList<Double>(); System.err.println("Load vector from " + path); Scanner sc = new Scanner(new BufferedReader(new FileReader(path))); while (sc.hasNext()) { String line = sc.nextLine(); if (line.isEmpty()) { break; }/* w ww .ja v a 2s . c o m*/ if (line.startsWith("%%")) { // detect matrix market format System.err.println(line); while (sc.nextLine().startsWith("%")) ; // skip the comment line, and the dimension info. continue; } arr.add(Double.parseDouble(line)); } System.err.println("Length: " + arr.size()); double[] ret = new double[arr.size()]; for (int i = 0; i < arr.size(); i++) ret[i] = arr.get(i); System.err.println("Done."); return ret; }
From source file:net.acesinc.convergentui.content.TextHttpMessageConverter.java
static String convertStreamToString(java.io.InputStream is) { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
From source file:com.aerofs.baseline.http.HttpUtils.java
public static String readStreamToString(InputStream in) throws IOException { Scanner scanner = new Scanner(in).useDelimiter("\\A"); return scanner.hasNext() ? scanner.next() : ""; }
From source file:org.dspace.authority.rest.RESTConnector.java
public static String convertStreamToString(InputStream is) { Scanner s = new Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
From source file:Main.java
public static double[][] readMatrixDouble(String path) throws FileNotFoundException { int n = 0, p = 0; System.err.println("Load matrix from " + path); Scanner sc = new Scanner(new BufferedReader(new FileReader(path))); while (sc.hasNext()) { String line = sc.nextLine(); if (line.startsWith("%")) { System.err.println(line); } else {/*from w w w .ja v a 2 s .c o m*/ String[] dim = line.split(" "); n = Integer.parseInt(dim[0]); p = Integer.parseInt(dim[1]); System.err.println("num rows: " + n + "\n" + "num cols: " + p); break; } } int count = 0; double[][] mat = new double[p][n]; int row = 0, col = 0; while (sc.hasNextLine()) { String line = sc.nextLine(); if (line.isEmpty()) { break; } Double val = Double.parseDouble(line); mat[col][row] = val; row++; if (row == n) { row = 0; col++; } count++; } if (count != n * p) { System.err.println("Parse fail: not enough elements. num rows: " + n + "\n" + "num cols: " + p + "\n nun elements: " + count); return null; } System.err.println("Done."); return mat; }
From source file:Main.java
public static float[][] readMatrix(String path) throws FileNotFoundException { int n = 0, p = 0; System.err.println("Load matrix from " + path); Scanner sc = new Scanner(new BufferedReader(new FileReader(path))); while (sc.hasNext()) { String line = sc.nextLine(); if (line.startsWith("%")) { System.err.println(line); } else {// ww w .j a v a2s . c om String[] dim = line.split(" "); n = Integer.parseInt(dim[0]); p = Integer.parseInt(dim[1]); System.err.println("num rows: " + n + "\n" + "num cols: " + p); break; } } int count = 0; float[][] mat = new float[p][n]; int row = 0, col = 0; while (sc.hasNextLine()) { String line = sc.nextLine(); if (line.isEmpty()) { break; } Float val = Float.parseFloat(line); mat[col][row] = val; row++; if (row == n) { row = 0; col++; } count++; } if (count != n * p) { System.err.println("Parse fail: not enough elements. num rows: " + n + "\n" + "num cols: " + p + "\n nun elements: " + count); return null; } System.err.println("Done."); return mat; }
From source file:Main.java
public static List<String> asList(String source, String separator) { Scanner scanner = new Scanner(source); scanner.useDelimiter(separator);/* w ww . ja va 2 s . co m*/ List<String> list = new ArrayList<String>(); while (scanner.hasNext()) { list.add(scanner.next()); } return list; }
From source file:org.knoxcraft.http.client.ClientMultipartFormPost.java
public static void upload(String url, String playerName, File jsonfile, File sourcefile) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*ww w . j av a 2 s . c o m*/ HttpPost httppost = new HttpPost(url); FileBody json = new FileBody(jsonfile); FileBody source = new FileBody(sourcefile); String jsontext = readFromFile(jsonfile); HttpEntity reqEntity = MultipartEntityBuilder.create() .addPart("playerName", new StringBody(playerName, ContentType.TEXT_PLAIN)) .addPart("jsonfile", json).addPart("sourcefile", source) .addPart("language", new StringBody("java", ContentType.TEXT_PLAIN)) .addPart("jsontext", new StringBody(jsontext, ContentType.TEXT_PLAIN)).addPart("sourcetext", new StringBody("public class Foo {\n int x=5\n}", ContentType.TEXT_PLAIN)) .build(); httppost.setEntity(reqEntity); System.out.println("executing request " + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); try { //System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { //System.out.println("Response content length: " + resEntity.getContentLength()); Scanner sc = new Scanner(resEntity.getContent()); while (sc.hasNext()) { System.out.println(sc.nextLine()); } sc.close(); EntityUtils.consume(resEntity); } } finally { response.close(); } } finally { httpclient.close(); } }