Example usage for java.util Scanner next

List of usage examples for java.util Scanner next

Introduction

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

Prototype

public String next() 

Source Link

Document

Finds and returns the next complete token from this scanner.

Usage

From source file:Main.java

public static List<String> asList(String source, String separator) {
    Scanner scanner = new Scanner(source);
    scanner.useDelimiter(separator);/*from   w  ww .  j a va  2  s.c  o  m*/
    List<String> list = new ArrayList<String>();
    while (scanner.hasNext()) {
        list.add(scanner.next());
    }
    return list;
}

From source file:com.msopentech.thali.utilities.webviewbridge.BridgeManager.java

/**
 * A useful utility for small streams. The stream will be closed by this method.
 *
 * Taken from http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
 * @param inputStream//  w w  w.  ja v  a 2  s .  c  o  m
 * @return Stream as a string
 */
public static String turnUTF8InputStreamToString(InputStream inputStream) {
    try {
        Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A");
        return scanner.hasNext() ? scanner.next() : "";
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:servlets.module.challenge.CsrfChallengeTargetJSON.java

@SuppressWarnings("resource")
static String extractPostRequestBody(HttpServletRequest request) throws IOException {
    if ("POST".equalsIgnoreCase(request.getMethod())) {
        Scanner s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }//from   w  w  w. j  av  a  2  s . co  m
    return "";
}

From source file:com.github.opengarageapp.GarageService.java

protected static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is, "UTF-8").useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

From source file:com.microsoft.tooling.msservices.helpers.ServiceCodeReferenceHelper.java

public static String getStringAndCloseStream(InputStream is) throws IOException {
    //Using the trick described in this link to read whole streams in one operation:
    //http://stackoverflow.com/a/5445161
    try {//from   w  w  w. ja va  2 s .  c om
        Scanner s = new Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    } finally {
        is.close();
    }
}

From source file:nu.yona.server.rest.RestClientErrorHandler.java

private static String convertStreamToString(InputStream is) {
    @SuppressWarnings("resource") // It's not on us to close this stream
    java.util.Scanner s = new Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

From source file:com.switchfly.inputvalidation.sanitizer.UrlStripHtmlSanitizer.java

private static void parse(List<NameValuePair> parameters, Scanner scanner) {
    scanner.useDelimiter(PARAMETER_SEPARATOR);
    while (scanner.hasNext()) {
        String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR, 2);
        if (nameValue.length == 0 || nameValue.length > 2) {
            throw new IllegalArgumentException("bad parameter");
        }/* ww  w.  j  a va2 s.  c  o m*/
        String name = ValidationStrategy.PROPERTY_NAME.cleanStrategy().validate(nameValue[0]);
        String value = null;
        if (nameValue.length == 2) {
            value = ValidationStrategy.DEFAULT_WEB_CONTENT_VALIDATION.validate(nameValue[1]);
        }
        parameters.add(new BasicNameValuePair(name, value));
    }
}

From source file:com.microsoft.tooling.msservices.helpers.ServiceCodeReferenceHelper.java

@NotNull
public static String getStringAndCloseStream(@NotNull InputStream is, @NotNull String charsetName)
        throws IOException {
    //Using the trick described in this link to read whole streams in one operation:
    //http://stackoverflow.com/a/5445161
    try {/*  w  ww . j  ava2  s .co m*/
        Scanner s = new Scanner(is, charsetName).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    } finally {
        is.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.
 *
 */// w  ww  .j  a v  a 2 s .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:pl.lewica.util.FileUtil.java

/**
 * Reads contents of a stream (e.g. SQL script) and splits it into separate statements.
 * IMPORTANT: The assumption is the statements are delimited by semicolons followed by new lines.
 * //from  ww  w  .ja  v a  2  s.c o  m
 * If you are using this method to convert a string with multiple SQL queries to individual statements,
 * make sure the semicolon-new line sequence doesn't exist anywhere inside the SQL statements, perhaps
 * somewhere in the middle of long varchars or text fields as they would be treated as SQL statement
 * delimiters and you would therefore get unexpected results. 
 * 
 * The method might be useful on Android that is unable to e.g. create multiple tables in one go.
 */
public static List<String> convertStreamToStrings(InputStream is, String delimiter) {
    List<String> result = new ArrayList<String>();
    Scanner s = new Scanner(is);
    s.useDelimiter(delimiter);

    while (s.hasNext()) {
        String line = s.next().trim();
        if (line.length() > 0) {
            result.add(line);
        }
    }
    s.close();

    return result;
}