Example usage for java.util Scanner Scanner

List of usage examples for java.util Scanner Scanner

Introduction

In this page you can find the example usage for java.util Scanner Scanner.

Prototype

public Scanner(ReadableByteChannel source) 

Source Link

Document

Constructs a new Scanner that produces values scanned from the specified channel.

Usage

From source file:com.cocktail.initializer.ItemReader.java

/**
 * Read items./*from   w  w w  .  j av  a2 s  . c  om*/
 *
 * @param <I>
 *            the generic type
 * @param path
 *            the path
 * @param itemMapper
 *            the item mapper
 * @return the list
 * @throws Exception
 *             the exception
 */
public static <I> List<I> readItems(String path, FieldSetMapper<I> itemMapper) throws Exception {

    ClassPathResource resource = new ClassPathResource(path);
    Scanner scanner = new Scanner(resource.getInputStream());
    String line = scanner.nextLine();
    scanner.close();

    FlatFileItemReader<I> itemReader = new FlatFileItemReader<I>();
    itemReader.setResource(resource);

    // DelimitedLineTokenizer defaults to | as its delimiter
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer("|");
    tokenizer.setNames(line.split("\\|"));
    tokenizer.setStrict(false);

    DefaultLineMapper<I> lineMapper = new DefaultLineMapper<I>();
    lineMapper.setLineTokenizer(tokenizer);
    lineMapper.setFieldSetMapper(itemMapper);
    itemReader.setLineMapper(lineMapper);
    itemReader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());
    itemReader.setLinesToSkip(1);
    itemReader.open(new ExecutionContext());

    List<I> items = new ArrayList<>();
    I item = null;

    do {

        item = itemReader.read();

        if (item != null) {
            items.add(item);
        }

    } while (item != null);

    return items;
}

From source file:io.fabric8.vertx.maven.plugin.utils.Prompter.java

public void startInteraction() {
    scanner = new Scanner(System.in);
}

From source file:com.taobao.datax.engine.tools.JobConfGenDriver.java

private static int showChoice(String header, List<String> plugins) {
    System.out.println(header);//from   www.ja v a2 s  .c o  m

    int idx = 0;
    for (String plugin : plugins) {
        System.out.println(String.format("%d\t%s", idx++,
                plugin.toLowerCase().replace("reader", "").replace("writer", "")));
    }

    System.out.print(String.format("Please choose [%d-%d]: ", 0, plugins.size() - 1));

    try {
        idx = Integer.valueOf(new Scanner(System.in).nextLine());
    } catch (Exception e) {
        // TODO: handle exception
        idx = -1;
    }

    return idx;
}

From source file:hammingcode.HammingCode.java

ArrayList<String> readFile(String filename) throws FileNotFoundException {
    Scanner input = new Scanner(new File(filename));
    ArrayList<String> inputList = new ArrayList();
    while (input.hasNext()) {
        inputList.add(input.nextLine());
    }//from   w ww. j ava  2  s  . c o m
    return inputList;
}

From source file:de.uni_freiburg.informatik.ultimate.licence_manager.util.ProcessUtils.java

public static void inheritIO(final InputStream src, final PrintStream dest) {
    new Thread(new Runnable() {
        public void run() {
            final Scanner sc = new Scanner(src);
            while (sc.hasNextLine()) {
                dest.println(sc.nextLine());
            }/*from  w ww  . j a  va2 s  . com*/
            sc.close();
        }
    }).start();
}

From source file:ca.weblite.xmlvm.VtableHelper.java

/**
 * Tries to find the actual function to use for the given mangled method name in the 
 * given header file./*from w w w  . jav a 2 s . c o m*/
 * @param searchPaths  The paths to search for headers.
 * @param headerFile The header file in which to start the search.  Its name is used
 * as the base for the mangled function name.
 * @param mangledMethodName The mangled method name.  This should only be the portion
 * pertaining to the method - not the class portion of a mangled function name.  It will
 * be concatenated to headerFile.getName()+"_" to form the full function name.
 * @return Either a VTable ID (which will have the form XMLVM_VTABLE_IDX_{funcname}) or
 * the actual function name if there is no vtable index constant defined.
 * @throws IOException 
 */
public static String resolveVirtualMethod(Project project, Path searchPaths, File headerFile,
        String mangledMethodName) throws IOException {
    String mangledClassName = headerFile.getName().replaceAll("\\.h$", "");
    String funcName = mangledClassName + "_" + mangledMethodName;
    String headerContents = FileUtils.readFileToString(headerFile);
    Scanner scanner = new Scanner(headerContents);
    int state = 0;
    String superClassHeader = null;
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if (state == 0 && line.startsWith("// Super Class:")) {
            state = 1;
        } else if (state == 1 && line.startsWith("#include \"")) {
            superClassHeader = line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\""));
            state = 0;
            break;
        }
    }

    String idxConstant = "XMLVM_VTABLE_IDX_" + funcName;
    String idxConstantSp = idxConstant + " ";
    if (headerContents.indexOf(idxConstantSp) > 0) {
        return idxConstant;
    } else if (headerContents.indexOf(funcName + "(") > 0) {
        return funcName;
    } else if (superClassHeader != null) {
        for (String p : searchPaths.list()) {
            File d = new File(p);
            File h = new File(d, superClassHeader);
            if (h.exists()) {
                return resolveVirtualMethod(project, searchPaths, h, mangledMethodName);
            }
        }
        throw new RuntimeException("Failed to find header file " + superClassHeader);
    } else {
        throw new RuntimeException(
                "Failed to find virtual method " + mangledMethodName + " in header " + headerFile);
    }
}

From source file:hu.petabyte.redflags.PassEncoder.java

@Test
public void test() {
    BCryptPasswordEncoder e = new BCryptPasswordEncoder();
    Scanner s = new Scanner(System.in);
    String i;//ww w  . jav a  2 s. co  m
    while (!(i = s.nextLine()).isEmpty()) {
        System.out.println("ENC: " + e.encode(i));
    }
    s.close();
}

From source file:cpd3314.buildit12.CPD3314BuildIt12.java

/**
 * Build a program that asks the user for a filename. It then reads a file
 * line-by-line, and outputs its contents in uppercase.
 *
 * Make sure that the program exits gracefully when the file does not exist.
 *
 *//* ww w  . j  a v  a2s  . c om*/
public static void doBuildIt1() {
    // Prompt the User for a Filename
    Scanner kb = new Scanner(System.in);
    System.out.println("Filename: ");
    String filename = kb.next();

    try {
        // Attempt to Access the File, this may cause an Exception
        File file = new File(filename);
        Scanner input = new Scanner(file);

        // Iterate through the File and Output in Upper Case
        while (input.hasNext()) {
            System.out.println(input.nextLine().toUpperCase());
        }
    } catch (IOException ex) {
        // If the File is Not Found, Tell the User and Exit Gracefully
        System.err.println("File not found: " + filename);
    }
}

From source file:view.UserInteractor.java

public void run(String... args) throws Exception {
    System.out.println(" ### Payment Tracker ### ");

    Scanner in = new Scanner(System.in);
    tryLoadFile(in);/*from   ww  w.  ja va 2 s .c  o m*/
    tryLoadExchangeRates();
    Thread viewerThread = startViewerThread();
    userInputCycle(in);
    viewerThread.interrupt();
}

From source file:com.dalelotts.rpn.ScannerTest.java

@Test
public void hasNextReturnsFalseForEmptyString() throws Exception {
    // Given//from   w w w. ja  v a2  s.c  o  m
    try (final InputStream inputStream = IOUtils.toInputStream("", "UTF-8")) {
        scanner = new Scanner(inputStream);

        // When
        final boolean hasNext = scanner.hasNext();

        // Then
        assertThat(hasNext, is(false));
    }
}