try (Scanner scanner = new Scanner(response)) { String responseBody = scanner.useDelimiter("\\A").next(); System.out.println(responseBody); }
Scanner sc = new Scanner(System.in); while (!sc.hasNext("exit")) { System.out.println( sc.hasNextInt() ? "(int) " + sc.nextInt() : sc.hasNextLong() ? "(long) " + sc.nextLong() : sc.hasNextDouble() ? "(double) " + sc.nextDouble() : sc.hasNextBoolean() ? "(boolean) " + sc.nextBoolean() : "(String) " + sc.next() ); }
Scanner fi = new Scanner("string 200 200.0"); fi.useLocale(Locale.US); while (fi.hasNext()) { if (fi.hasNextInt()) { System.out.println("Integer: " + fi.nextInt()); } else if (fi.hasNextDouble()) { System.out.println("Double: " + fi.nextDouble()); } else { System.out.println("String: " + fi.next()); } }
try (Scanner in = new Scanner(System.in)) { while (in.hasNextLine()) { try { String line = in.nextLine(); Scanner lineScan = new Scanner(line); BigDecimal moneyInput = lineScan.nextBigDecimal(); String currency = lineScan.next(); // do something } catch (NoSuchElementException | IllegalStateException e) { System.err.print("Please enter the VALUE followed by the CURRENCY"); } } }
Scanner scanner = new Scanner( new File("poem.txt") ); String text = scanner.useDelimiter("\\A").next(); scanner.close(); // Put this call in a finally block
private ProcessInformations(Scanner sc, boolean windows, boolean macOrAix) { super(); if (windows) { final StringBuilder imageNameBuilder = new StringBuilder(sc.next()); while (!sc.hasNextInt()) { imageNameBuilder.append(' ').append(sc.next()); pid = sc.nextInt(); if ("Console".equals(sc.next())) { sc.next(); final String memory = sc.next(); cpuPercentage = -1; memPercentage = -1; rss = -1; tty = null; sc.next(); sc.skip(WINDOWS_STATE_PATTERN); stat = null; final StringBuilder userBuilder = new StringBuilder(sc.next()); while (!sc.hasNext(WINDOWS_CPU_TIME_PATTERN)) { userBuilder.append(' ').append(sc.next()); cpuTime = sc.next(); command = imageNameBuilder.append(" (").append(sc.nextLine().trim()).append(')') .toString(); } else { user = sc.next();
protected boolean readLine(){ if (!scan.hasNext()){ return false; String cmd = scan.next().toLowerCase(); if (cmd.startsWith("#")){ String name = scan.next(); startMaterial(name); }else if (cmd.equals("ka")){ String path = scan.next(); alphaMap = loadTexture(path); transparent = true; }else if (cmd.equals("illum")){ int mode = scan.nextInt();
/** * Reads the next token from standard input, parses it as an integer, and returns the integer. * * @return the next integer on standard input * @throws NoSuchElementException if standard input is empty * @throws InputMismatchException if the next token cannot be parsed as an {@code int} */ public static int readInt() { try { return scanner.nextInt(); } catch (InputMismatchException e) { String token = scanner.next(); throw new InputMismatchException("attempts to read an 'int' value from standard input, " + "but the next token is \"" + token + "\""); } catch (NoSuchElementException e) { throw new NoSuchElementException("attemps to read an 'int' value from standard input, " + "but no more tokens are available"); } }
static String convertStreamToString(java.io.InputStream is) { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
String text = "row1\tblah\tblah\tblah\n" + "row2\t1\t2\t3\t4\r\n" + "row3\tA\tB\tC\r" + "row4"; System.out.println(text); // row1 blah blah blah // row2 1 2 3 4 // row3 A B C // row4 List<List<String>> input = new ArrayList<List<String>>(); Scanner sc = new Scanner(text); while (sc.hasNextLine()) { Scanner lineSc = new Scanner(sc.nextLine()).useDelimiter("\t"); List<String> line = new ArrayList<String>(); while (lineSc.hasNext()) { line.add(lineSc.next()); } input.add(line); } System.out.println(input); // [[row1, blah, blah, blah], [row2, 1, 2, 3, 4], [row3, A, B, C], [row4]]
public static Exchange createInstanceFromDefaultInput() { Exchange mercadoBitcoin = ExchangeFactory.INSTANCE.createExchange(MercadoBitcoinExchange.class.getName()); ExchangeSpecification exchangeSpecification = mercadoBitcoin.getExchangeSpecification(); Scanner s = new Scanner(System.in); System.out.println("Enter your API key (chave):"); exchangeSpecification.setApiKey(s.next().trim().toLowerCase()); System.out.println("Enter your secret code (código):"); exchangeSpecification.setSecretKey(s.next().trim().toLowerCase()); System.out.println("Enter your PIN (IMPORTANT: This is your PIN, not your user password):"); exchangeSpecification.setPassword(s.next().trim().toLowerCase()); exchangeSpecification.setUserName("<optional username>"); System.out.println("Please wait..."); return mercadoBitcoin; } }
Scanner scanner = new Scanner( new File("poem.txt"), "UTF-8" ); String text = scanner.useDelimiter("\\A").next(); scanner.close(); // Put this call in a finally block