Example usage for java.util Scanner useDelimiter

List of usage examples for java.util Scanner useDelimiter

Introduction

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

Prototype

public Scanner useDelimiter(String pattern) 

Source Link

Document

Sets this scanner's delimiting pattern to a pattern constructed from the specified String .

Usage

From source file:MainClass.java

public static void main(String args[]) throws IOException {

    FileWriter fout = new FileWriter("test.txt");
    fout.write("2, 3.4,    5,6, 7.4, 9.1, 10.5, done");
    fout.close();//from   w ww . j a v a 2s .co  m

    FileReader fin = new FileReader("Test.txt");
    Scanner src = new Scanner(fin);
    // Set delimiters to space and comma.
    // ", *" tells Scanner to match a comma and zero or more spaces as
    // delimiters.

    src.useDelimiter(", *");

    // Read and sum numbers.
    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            System.out.println(src.nextDouble());
        } else {
            break;
        }
    }
    fin.close();
}

From source file:org.slc.sli.encryption.tool.Encryptor.java

/**
 * New functionality allows encrypting using the same algorithm
 * the api uses for encrypting PII fields in MongoDB.
 *
 * for example:/*from   w w w  .  j  av a  2s . c om*/
  java -jar encryption-tool.java --decrypt
  ../data-access/dal/keyStore/localKeyStore.jks
  ../data-access/dal/keyStore/localEncryption.properties
  aDvgicSt81j/YdPUhvr4Ig==
        
 * or to encrypt:
  java -jar encryption-tool.java
  ../data-access/dal/keyStore/localKeyStore.jks
  ../data-access/dal/keyStore/localEncryption.properties
  Lauretta
        
 * you can also exclude the message at the end, then this program works
 * in streaming mode: every line received from System.in is en/decrypted
 * and returned to System.out
        
 * @param args
 * @throws Exception
 */

public static void main(String[] args) throws Exception {
    PrintStream out = System.out;
    if (args.length < 2 || args.length > 6) {
        out.println("For encryption:");
        out.println(
                "Usage: java -jar encryption-tool.jar <keystore_location> <keystore_password> <key_alias> <key_password> <string>");
        out.println("For decryption");
        out.println(
                "Usage: java -jar encryption-tool.jar --decrypt <keystore_location> <keystore_password> <key_alias> <key_password> <string>");
        out.println();
        out.println("Using a properties file: \n  Property names must end with the following values. ");
        out.println("  " + KEYSTOREPASS + " (required)");
        out.println("  " + DALKEYALIAS + " (required)");
        out.println("  " + DALKEYPASS + " (required) ");
        out.println("  " + DALINITVEC + " (optional)");
        out.println("  " + DALALGORITHM + "(optional, defaults to \"AES/CBC/PKCS5Padding\")");

        out.println("Encryption:");
        out.println(
                "  Usage: java jar encyption-tool-1.0-jar <keystore_location> <properties_file> [<string>]");
        out.println("Decryption:");
        out.println(
                "  Usage: java jar encyption-tool-1.0-jar -decrypt <keystore_location> <properties_file> [<string>]");

        return;
    }

    boolean decrypt = false;
    String[] effectiveArgs = args;
    if (args[0].equals("--decrypt")) {
        decrypt = true;
        effectiveArgs = new String[args.length - 1];
        System.arraycopy(args, 1, effectiveArgs, 0, args.length - 1);
    }

    String propertiesLocation = null;
    String keystoreLocation = null;
    String keystorePassword = null;
    String keyAlias = null;
    String keyPassword = null;
    String message = null;
    String initVector = null;
    String algorithm = "AES/CBC/PKCS5Padding";

    if (args.length == 5 || args.length == 6) {
        //OLDER FUNCTIONALITY
        keystoreLocation = effectiveArgs[0];
        keystorePassword = effectiveArgs[1];
        keyAlias = effectiveArgs[2];
        keyPassword = effectiveArgs[3];
        message = effectiveArgs[4];

        Encryptor encryptor = new Encryptor(keystoreLocation, keystorePassword);

        if (!decrypt) {
            String encryptedString = encryptor.encrypt(keyAlias, keyPassword, message);
            out.println("Encrypted string for " + message + " is: " + encryptedString + "\n");
        } else {
            String decryptedString = encryptor.decrypt(keyAlias, keyPassword, message);
            out.println("Descrypted string for " + message + " is: " + decryptedString + "\n");
        }

    } else {
        //NEWER FUNCTIONALITY
        keystoreLocation = effectiveArgs[0];
        propertiesLocation = effectiveArgs[1];

        if (effectiveArgs.length == 3) {
            message = effectiveArgs[2];
        } else {
            message = null;
        }

        Map<String, String> properties = parsePropertiesFile(propertiesLocation);

        keystorePassword = properties.get(KEYSTOREPASS);
        keyPassword = properties.get(DALKEYPASS);
        keyAlias = properties.get(DALKEYALIAS);

        if (properties.containsKey(DALINITVEC)) {
            initVector = properties.get(DALINITVEC);
        }
        if (properties.containsKey(DALALGORITHM)) {
            algorithm = properties.get(DALALGORITHM);
        }

        Encryptor encryptor = new Encryptor(keystoreLocation, keystorePassword);

        if (!decrypt) {
            if (message != null) {
                String encrypted = encryptor.encrypt(keyAlias, keyPassword, algorithm, initVector, message);
                out.println(encrypted);
            } else {
                Scanner s = new Scanner(System.in);
                s.useDelimiter("\n");
                while (s.hasNext()) {
                    String cleartext = s.next();
                    String encrypted = encryptor.encrypt(keyAlias, keyPassword, algorithm, initVector,
                            cleartext);
                    out.println(encrypted);
                }
            }
        } else {
            if (message != null) {
                String decrypted = encryptor.decrypt(keyAlias, keyPassword, algorithm, initVector, message);
                out.println(decrypted);
            } else {
                Scanner s = new Scanner(System.in);
                s.useDelimiter("\n");
                while (s.hasNext()) {
                    String ciphertext = s.next().trim();
                    String decrypted = encryptor.decrypt(keyAlias, keyPassword, algorithm, initVector,
                            ciphertext);
                    out.println(decrypted);
                }
            }
        }
    }
}

From source file:Main.java

static void parseLine(String line) {
    Scanner lineScanner = new Scanner(line);
    lineScanner.useDelimiter("\\s*,\\s*");
    String name = lineScanner.next();
    int age = lineScanner.nextInt();
    boolean isCertified = lineScanner.nextBoolean();
    System.out.println("It is " + isCertified + " that " + name + ", age " + age + ", is certified.");
}

From source file:Main.java

private static Scanner wordScanner(String text) {
    Scanner s = new Scanner(text);
    s.useDelimiter(WHITESPACE_PATTERN);
    return s;//  w  w w .j  a  v  a  2s . c o  m
}

From source file:Main.java

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

From source file:Main.java

/**
 * Reads a file relative to the dir this app was started from.
 * @param filename relative filename to load
 * @return entire file as a String//from w w  w. j a v  a  2 s.  co  m
 * @throws FileNotFoundException if file not found!
 */
public static String readFile(String filename) throws FileNotFoundException {

    String startDir = System.getProperty("user.dir");
    File propertyFile = new File(startDir, filename);

    Scanner scan = new Scanner(new FileInputStream(propertyFile));
    scan.useDelimiter("\\Z");
    String content = scan.next();
    scan.close();

    return content;
}

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");
        }/*www .j a  va  2  s  .c  om*/
        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:hu.sztaki.incremental.ml.streaming.imsr.MatrixVectorPairSource.java

private static Scanner initCsvScanner(Scanner s) {
    s.useLocale(Locale.ENGLISH);//from w  w  w.  ja  va  2s .  c  o m
    s.useDelimiter("(\\s+|\\s*,\\s*)");
    return s;
}

From source file:Main.java

/**
 * Adds all parameters within the Scanner to the list of <code>parameters</code>.
 * For example,a scanner containing the string <code>a=1&b=2&c=3</code> would
 * add the {@link org.apache.http.NameValuePair NameValuePairs} a=1, b=2, and c=3 to the
 * list of parameters./* ww w  . j  a  va2 s .  com*/
 *
 * @param parameters List to add parameters to.
 * @param scanner    Input that contains the parameters to parse.
 */
public static void parse(final List<NameValuePair> parameters, final Scanner scanner) {
    scanner.useDelimiter(PARAMETER_SEPARATOR);
    while (scanner.hasNext()) {
        String name = null;
        String value = null;
        String token = scanner.next();
        int i = token.indexOf(NAME_VALUE_SEPARATOR);
        if (i != -1) {
            name = token.substring(0, i).trim();
            value = token.substring(i + 1).trim();
        } else {
            name = token.trim();
        }
        parameters.add(new BasicNameValuePair(name, value));
    }
}

From source file:Main.java

/**
 * Adds all parameters within the Scanner to the list of
 * <code>parameters</code>, as encoded by <code>encoding</code>. For
 * example, a scanner containing the string <code>a=1&b=2&c=3</code> would
 * add the {@link Pair<String, String> NameValuePairs} a=1, b=2, and c=3 to the
 * list of parameters./*  ww w  . jav  a2  s. c om*/
 *
 * @param parameters
 *            List to add parameters to.
 * @param scanner
 *            Input that contains the parameters to parse.
 * @param encoding
 *            Encoding to use when decoding the parameters.
 */
public static void parse(final List<Pair<String, String>> parameters, final Scanner scanner,
        final String encoding) {
    scanner.useDelimiter(PARAMETER_SEPARATOR);
    while (scanner.hasNext()) {
        final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR);
        if (nameValue.length == 0 || nameValue.length > 2)
            throw new IllegalArgumentException("bad parameter");

        final String name = decode(nameValue[0], encoding);
        String value = null;
        if (nameValue.length == 2)
            value = decode(nameValue[1], encoding);
        parameters.add(new Pair<String, String>(name, value));
    }
}