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:io.github.mikesaelim.arxivoaiharvester.CommandLineInterface.java

/**
 * Construct a "GetRecord" request from user inputs.
 *//*from   w  w  w.ja  va 2  s .co m*/
private static GetRecordRequest constructGetRecordRequest(Scanner scanner) {
    System.out.println("What is the identifier for the record you wish to get?  "
            + "For example, \"oai:arXiv.org:1302.2146\".");

    while (true) {
        String identifier = scanner.nextLine().trim();

        if (isBlank(identifier)) {
            System.out.println("    Please enter something.  Try again?");
        } else {
            try {
                return new GetRecordRequest(identifier);
            } catch (URISyntaxException e) {
                System.out.println("    Sorry, identifier \"" + identifier + "\" did not result in a valid "
                        + "request URI.  Try again?");
            }
        }
    }
}

From source file:ec.edu.ucuenca.dcc.sld.HttpUtils.java

public static synchronized String Http(String s) throws SQLException, IOException {

    String resp = "";
    final URL url = new URL(s);
    final URLConnection connection = url.openConnection();
    connection.setConnectTimeout(60000);
    connection.setReadTimeout(60000);/* ww w .j  a v  a  2s  .c  om*/
    connection.addRequestProperty("User-Agent",
            "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0");
    connection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    final Scanner reader = new Scanner(connection.getInputStream(), "UTF-8");
    while (reader.hasNextLine()) {
        final String line = reader.nextLine();
        resp += line + "\n";
    }
    reader.close();

    return resp;
}

From source file:com.github.mavogel.ilias.utils.IOUtils.java

/**
 * Just reads the next line from System in and deleting whitespaces.
 *
 * @return the line/*w w  w. j av a 2s.c o m*/
 */
public static String readLine() {
    Scanner scanner = new Scanner(System.in);
    String nextLine = scanner.nextLine();
    return StringUtils.deleteWhitespace(nextLine);
}

From source file:com.pinterest.terrapin.tools.TerrapinAdmin.java

@VisibleForTesting
protected static boolean confirmFileSetDeletion(String fileSet, InputStream inputStream) {
    System.out.print(String.format("are you sure to delete %s? [y/n]: ", fileSet));
    Scanner scanner = new Scanner(inputStream);
    if (scanner.hasNextLine()) {
        String input = scanner.nextLine();
        if (input.length() > 0 && input.equalsIgnoreCase("y")) {
            return true;
        }// w w  w  . j  ava  2s.co m
    }
    return false;
}

From source file:com.pinterest.terrapin.tools.TerrapinAdmin.java

@VisibleForTesting
protected static boolean confirmFileSetRollbackVersion(String fileSet, FileSetInfo fileSetInfo,
        int versionIndex, InputStream inputStream) {
    String rollbackHdfs = fileSetInfo.oldServingInfoList.get(versionIndex).hdfsPath;
    System.out.print(String.format("are you sure to rollback %s from %s to %s? [y/n]: ", fileSet,
            fileSetInfo.servingInfo.hdfsPath, rollbackHdfs));
    Scanner scanner = new Scanner(inputStream);
    if (scanner.hasNextLine()) {
        String input = scanner.nextLine();
        if (input.length() > 0 && input.equalsIgnoreCase("y")) {
            return true;
        }//from  w ww. j ava2 s  .  c om
    }
    return false;
}

From source file:dm_p2.KMEANS.java

public static void generateLinkedHashMap(String filename) {
    String filePath = new File("").getAbsolutePath();
    try {// w  ww  . ja v  a2 s  .  c om
        Scanner s = new Scanner(new File(filePath + "/src/dm_p2/" + filename));
        while (s.hasNext()) {
            String inputLine = s.nextLine();
            String[] splitData = inputLine.split("\t");
            int geneId = Integer.parseInt(splitData[0]);
            int valgeneId = Integer.parseInt(splitData[1]);
            expressionValues = new ArrayList<Double>();
            for (int i = 2; i < splitData.length; i++) {
                expressionValues.add(Double.parseDouble(splitData[i]));
            }
            linkedHashMap.put(geneId, expressionValues);
            if (valgeneId != -1) {
                validationMap.put(geneId, valgeneId);
            }
        }
        //System.out.println(linkedHashMap.entrySet());
        //System.out.println(validationMap.entrySet());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.dm.estore.common.utils.FileUtils.java

/**
 * Save text file from classpath to file.
 *
 * @param resource Classpath text resource
 * @param outputFile Output file//from  w w  w.  j a  v a  2  s  . co m
 * @param processor Optional line processor
 * @param encoding Text encoding
 * @throws IOException Exception
 */
public static void saveTextFileFromClassPath(String resource, String outputFile, String encoding,
        TextStreamProcessor processor) throws IOException {

    final InputStream inputStream = FileUtils.class.getResourceAsStream(resource);
    final Scanner scanner = new Scanner(inputStream, encoding);
    final Writer out = new OutputStreamWriter(new FileOutputStream(outputFile), encoding);
    try {

        while (scanner.hasNextLine()) {
            final String nextLine = scanner.nextLine();
            out.write(processor != null ? processor.process(nextLine) : nextLine);
            out.write(NEW_LINE_SEP);
        }

    } finally {
        out.close();
        scanner.close();
    }

}

From source file:com.pinterest.terrapin.tools.TerrapinAdmin.java

@VisibleForTesting
protected static int selectFileSetRollbackVersion(FileSetInfo fileSetInfo, InputStream inputStream) {
    int size = fileSetInfo.oldServingInfoList.size();
    if (fileSetInfo.numVersionsToKeep < 2 || fileSetInfo.oldServingInfoList.size() < 1) {
        throw new IllegalArgumentException("no available version for rollback");
    }/*w  w  w.  ja  va2  s.c o  m*/
    System.out.println("available versions for rollback:");
    for (int i = 0; i < size; i++) {
        FileSetInfo.ServingInfo servingInfo = fileSetInfo.oldServingInfoList.get(i);
        System.out.println(String.format("[%d] %s", i, servingInfo.hdfsPath));
    }
    if (size > 1) {
        System.out.print(String.format("choose a version [0-%d]: ", size - 1));
    } else {
        System.out.print("choose a version [0]: ");
    }
    Scanner scanner = new Scanner(inputStream);
    if (scanner.hasNextLine()) {
        String input = scanner.nextLine();
        if (input.length() > 0) {
            int index = Integer.valueOf(input);
            if (index >= 0 && index < size) {
                return index;
            }
            throw new IllegalArgumentException(
                    String.format("version index should between 0 and %d", size - 1));
        }
    }
    throw new IllegalArgumentException("version index not specified");
}

From source file:css.variable.converter.CSSVariableConverter.java

/**
 * Collect variables from main file/*from www .j a  v  a 2  s.  com*/
 *
 * @param mainFile File with ":root" identifier for variables.
 * @return
 */
private static ArrayList<CSSVar> getCSSVars(File mainFile, String identifier) {
    ArrayList<CSSVar> cSSVarsList = new ArrayList<CSSVar>();
    boolean isInVarDefinition = false;
    boolean isFinished = false;

    try {
        Scanner fileReader = new Scanner(mainFile);

        // Loop until end of text file, or outside of :root 
        while (fileReader.hasNextLine() && !isFinished) {
            String currentLine = fileReader.nextLine();

            if (isInVarDefinition) {

                // I identify all of my variables with --
                if (currentLine.contains("--")) {

                    int varNameBegIndex = currentLine.indexOf("--");
                    int varNameEndIndex = currentLine.indexOf(":");
                    String varName = currentLine.substring(varNameBegIndex, varNameEndIndex);

                    int varValueBegIndex = currentLine.indexOf(":") + 1;
                    int varValueEndIndex = currentLine.indexOf(";");
                    String varValue = currentLine.substring(varValueBegIndex, varValueEndIndex);

                    varName = varName.trim();
                    varValue = varValue.trim();
                    cSSVarsList.add(new CSSVar(varName, varValue));
                }

                if (currentLine.contains("}")) {
                    isInVarDefinition = false;
                    isFinished = true;
                }
            } else {
                if (currentLine.contains(identifier)) {
                    isInVarDefinition = true;
                }
            }

        }
        fileReader.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex);
    }

    return cSSVarsList;
}

From source file:com.google.api.ads.adwords.jaxws.extensions.kratu.KratuMain.java

/**
 * Prints the sample properties file on the default output.
 *///from  w w w. ja v a2  s  .  co m
private static void printSamplePropertiesFile() {

    System.out.println("\n  File: kratubackend-sample.properties example");

    ClassPathResource sampleFile = new ClassPathResource(CLASSPATH_AW_REPORT_MODEL_PROPERTIES_LOCATION);
    Scanner fileScanner = null;
    try {
        fileScanner = new Scanner(sampleFile.getInputStream());
        while (fileScanner.hasNext()) {
            System.out.println(fileScanner.nextLine());
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileScanner != null) {
            fileScanner.close();
        }
    }
}