static String convertStreamToString(java.io.InputStream is) { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
class Test { public static void main(String[] args) { int i = (new java.util.Scanner(System.in).nextInt()); } }
Scanner scanner = new Scanner(myString); while (scanner.hasNextLine()) { String line = scanner.nextLine(); // process the line } scanner.close();
/** * Reads the Label file, the file which contains the Mary context features, creates an scanner object and calls getTargets * * @param LabFile * LabFile * @param htsData * htsData * @throws Exception * Exception * @return targets */ public static List<Target> getTargetsFromFile(String LabFile, HMMData htsData) throws Exception { List<Target> targets = null; Scanner s = null; try { /* parse text in label file */ s = new Scanner(new BufferedReader(new FileReader(LabFile))); targets = getTargets(s, htsData); } catch (FileNotFoundException e) { System.err.println("FileNotFoundException: " + e.getMessage()); } finally { if (s != null) s.close(); } return targets; }
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() ); }
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class TestScanner { public static void main(String[] args) throws FileNotFoundException { Scanner scanner = new Scanner(new File("/Users/pankaj/abc.csv")); scanner.useDelimiter(","); while(scanner.hasNext()){ System.out.print(scanner.next()+"|"); } scanner.close(); } }
/** * 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; }
@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(); }
private static String readKey(String key) { Scanner stdin = new Scanner(System.in); System.out.println("Choose a Spider demo:"); for (Map.Entry<String, Class> classEntry : clazzMap.entrySet()) { System.out.println(classEntry.getKey()+"\t" + classEntry.getValue() + "\t" + urlMap.get(classEntry.getKey())); } while (key == null) { key = stdin.nextLine(); if (clazzMap.get(key) == null) { System.out.println("Invalid choice!"); key = null; } } return key; } }
public static int testUserInput(InputStream in,PrintStream out) { Scanner keyboard = new Scanner(in); out.println("Give a number between 1 and 10"); int input = keyboard.nextInt(); while (input < 1 || input > 10) { out.println("Wrong number, try again."); input = keyboard.nextInt(); } return input; }