Example usage for java.util Scanner nextLine

List of usage examples for java.util Scanner nextLine

Introduction

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

Prototype

public String nextLine() 

Source Link

Document

Advances this scanner past the current line and returns the input that was skipped.

Usage

From source file:com.thoughtworks.go.util.OperatingSystem.java

private static String detectCompleteName() {
    String[] command = { "python", "-c", "import platform;print(platform.linux_distribution())" };
    try {/*from w  w  w.java2s .co m*/
        Process process = Runtime.getRuntime().exec(command);
        Scanner scanner = new Scanner(process.getInputStream());
        String line = scanner.nextLine();
        OS_COMPLETE_NAME = cleanUpPythonOutput(line);
    } catch (Exception e) {
        try {
            OS_COMPLETE_NAME = readFromOsRelease();
        } catch (Exception ignored) {
            OS_COMPLETE_NAME = OS_FAMILY_NAME;
        }
    }
    return OS_COMPLETE_NAME;
}

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;
        }/*from w  w w  . j a  v  a 2  s. com*/
        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:example.transaction.Client.java

private static void acceptInputFromUser(Session senderSession, MessageProducer sender) throws JMSException {
    System.out.println("Type a message. Type COMMIT to send to receiver, type ROLLBACK to cancel");
    Scanner inputReader = new Scanner(System.in);

    while (true) {
        String line = inputReader.nextLine();
        if (line == null) {
            System.out.println("Done!");
            break;
        } else if (line.length() > 0) {
            if (line.trim().equals("ROLLBACK")) {
                System.out.println("Rolling back...");
                senderSession.rollback();
                System.out.println("Messages have been rolledback");
            } else if (line.trim().equals("COMMIT")) {
                System.out.println("Committing... ");
                senderSession.commit();//from www. j a  va2  s  . com
                System.out.println("Messages should have been sent");
            } else {
                TextMessage message = senderSession.createTextMessage();
                message.setText(line);
                System.out.println("Batching up:'" + message.getText() + "'");
                sender.send(message);
            }
        }
    }
}

From source file:org.knoxcraft.http.client.ClientMultipartFormPost.java

private static String readFromFile(File file) throws IOException {
    StringBuilder buf = new StringBuilder();
    Scanner scan = new Scanner(new FileInputStream(file));
    while (scan.hasNextLine()) {
        String line = scan.nextLine();
        buf.append(line);//w w w.ja  v a2  s.c  o m
        buf.append("\n");
    }
    scan.close();
    return buf.toString();
}

From source file:br.univali.ps.fuzzy.portugolFuzzyCorretor.control.FileController.java

public static String formatarArquivoTexto(File file) throws FileNotFoundException {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    StringBuilder text = new StringBuilder();
    String NL = System.getProperty("line.separator");
    Scanner scanner = new Scanner(file, "ISO-8859-1");
    while (scanner.hasNextLine()) {
        text.append(scanner.nextLine() + NL);
    }/*from  w  w w . jav a  2 s  .com*/
    scanner.close();
    return text.toString();
}

From source file:com.ExtendedAlpha.SWI.SeparatorLib.Separator.java

public static String getStringFromStream(InputStream stream) {
    Scanner x = new Scanner(stream);
    String str = "";
    while (x.hasNextLine()) {
        str += x.nextLine() + "\n";
    }/* www .j  ava  2 s  .  c o  m*/
    x.close();
    return str.trim();
}

From source file:TextFileTest.java

/**
 * Reads an array of employees from a scanner
 * @param in the scanner//ww  w. j av a 2  s .c o  m
 * @return the array of employees
 */
private static Employee[] readData(Scanner in) {
    // retrieve the array size
    int n = in.nextInt();
    in.nextLine(); // consume newline

    Employee[] employees = new Employee[n];
    for (int i = 0; i < n; i++) {
        employees[i] = new Employee();
        employees[i].readData(in);
    }
    return employees;
}

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  v  a  2  s.c  o m*/
            sc.close();
        }
    }).start();
}

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

/**
 * Read items.//w  ww .ja v a2s  .  c o m
 *
 * @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: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 {/*  w w w  .  j  av a  2 s . com*/
            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;
}