static String convertStreamToString(java.io.InputStream is) { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
/** * visible width for the given string. * * for example: "abc\n1234"'s width is 4. * * @param string the given string * @return visible width */ private static int width(String string) { int maxWidth = 0; try (Scanner scanner = new Scanner(new StringReader(string))) { while (scanner.hasNextLine()) { maxWidth = max(length(scanner.nextLine()), maxWidth); } } return maxWidth; }
Scanner scanner = new Scanner(myString); while (scanner.hasNextLine()) { String line = scanner.nextLine(); // process the line } scanner.close();
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() ); }
public static boolean isInteger(String s, int radix) { Scanner sc = new Scanner(s.trim()); if(!sc.hasNextInt(radix)) return false; // we know it starts with a valid int, now make sure // there's nothing left! sc.nextInt(radix); return !sc.hasNext(); }
public JavaVersion(String version) { versions = new ArrayList<>(); Scanner s = new Scanner(version).useDelimiter("[^\\d]+"); while (s.hasNext()) { versions.add(s.nextInt()); } }
Scanner scanner = new Scanner(a); scanner.useDelimiter(","); scanner.close();
Scanner sc = new Scanner(System.in); int number; do { System.out.println("Please enter a positive number!"); while (!sc.hasNextInt()) { System.out.println("That's not a number!"); sc.next(); // this is important! } number = sc.nextInt(); } while (number <= 0); System.out.println("Thank you! Got " + number);
@Override public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain chain) throws IOException { resource = chain.transform(request, resource); if (!this.fileExtension.equals(StringUtils.getFilenameExtension(resource.getFilename()))) { return resource; } byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream()); String content = new String(bytes, DEFAULT_CHARSET); if (!content.startsWith(MANIFEST_HEADER)) { if (logger.isTraceEnabled()) { logger.trace("Skipping " + resource + ": Manifest does not start with 'CACHE MANIFEST'"); } return resource; } @SuppressWarnings("resource") Scanner scanner = new Scanner(content); LineInfo previous = null; LineAggregator aggregator = new LineAggregator(resource, content); while (scanner.hasNext()) { String line = scanner.nextLine(); LineInfo current = new LineInfo(line, previous); LineOutput lineOutput = processLine(current, request, resource, chain); aggregator.add(lineOutput); previous = current; } return aggregator.createResource(); }
/** * Reads and returns the remainder of this input stream, as a string. * * @return the remainder of this input stream, as a string */ public String readAll() { if (!scanner.hasNextLine()) return ""; String result = scanner.useDelimiter(EVERYTHING_PATTERN).next(); // not that important to reset delimeter, since now scanner is empty scanner.useDelimiter(WHITESPACE_PATTERN); // but let's do it anyway return result; }
public void readShapeData() throws IOException { Scanner in = new Scanner(System.in); try { System.out.println("Enter the width of the Rectangle: "); width = in.nextDouble(); System.out.println("Enter the height of the Rectangle: "); height = in.nextDouble(); } finally { in.close(); } }
LineInfoGenerator(String content) { this.scanner = new Scanner(content); }
private void mapLinesToOffsets() throws IOException { try (Scanner scanner = new Scanner(filePath)) { int currentGlobalOffset = 0; while (scanner.hasNextLine()) { lineToOffset.add(currentGlobalOffset); currentGlobalOffset += getLineLengthWithLineSeparator(scanner); } } }