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:Main.java

public static Map<String, List<String>> createDictionary(Context context) {
    try {//from ww  w.j  a  va  2 s .  c o  m
        AssetManager am = context.getAssets();
        InputStream is = am.open(DICTIONARY_FILENAME);
        Scanner reader = new Scanner(is);
        Map<String, List<String>> map = new HashMap<String, List<String>>();

        while (reader.hasNextLine()) {
            String word = reader.nextLine();
            char[] keyArr = word.toCharArray();
            Arrays.sort(keyArr);
            String key = String.copyValueOf(keyArr);

            List<String> wordList = map.get(key);
            if (wordList == null) {
                wordList = new LinkedList<String>();
            }
            wordList.add(word);
            map.put(key, wordList);
        }
        reader.close();
        return map;
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    return null;
}

From source file:magixcel.Main.java

private static void setup() {
    Scanner s = new Scanner(System.in);
    boolean doContinue = false;

    //        Intro
    printLogo();//from  ww w.  j  a  va2  s  . c  o  m
    System.out.println("Welcome to MagiXcel");
    System.out.println("Current encoding is " + Charset.defaultCharset());

    //        Table name
    while (!doContinue) {
        System.out.println("Type your desired table name:");
        if (s.hasNextLine()) {
            Table.setTableName(s.nextLine());
        }

        doContinue = isThisOk(s);
    }
    doContinue = false;

    //        File path
    String path = "";
    while (!doContinue) {
        System.out.println("Enter FULL path of .csv or .txt file you wish to convert");
        if (s.hasNextLine()) {
            path = StringEscapeUtils.escapeJava(s.nextLine());
        }
        FileMan.setPath(path);
        if (FileMan.currentPathIsValid()) {
            doContinue = isThisOk(s);
        } else {
            System.out.println("ERROR: Invalid Path");
            doContinue = false;
        }

    }
    doContinue = false;

    //        File setup
    FileMan.getLinesFromFile();
    Table.setColumnNames(FileMan.takeFirstRow());
    FileMan.addRowsToTable();

    //        Choose output mode
    System.out.println("Choose output mode:");
    OutputFormat.printOutputOptionsNumbered();
    int selectedOption = -1;
    while (!doContinue) {
        if (s.hasNextLine()) {
            selectedOption = Integer.parseInt(s.nextLine());
        }
        doContinue = OutputFormat.chooseOption(selectedOption, s);
    }

    doContinue = false;
    OutputFormat.format();

    FileMan.writeToFile();

    System.out.println("Thank you for using MagiXcel!");
    System.out.println("Press Enter to exit");
    s.nextLine();
    s.close();

}

From source file:IO.java

public static double[][] readDoubleMat(File file, int row, int col) {
    double[][] data = new double[row][col];
    try {/*from   w  w w.jav  a 2  s.  c  o  m*/
        Scanner sc = new Scanner(file);
        sc.useDelimiter(",|\\n");

        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++) {
                data[i][j] = Double.parseDouble(sc.next());
            }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return data;
}

From source file:formatMessage.VerifyInputScanner.java

/**
 * De verifyString methode is echt heel moeilijk want eignelijk is alles een string misschien moeten we hier anders op testen 
 * door bijvoorbeeld te stellen dat er geen cijfers inmogen? 
 * @return // ww  w  .j a  va 2 s  . co m
 */
public static String verifyString() {

    while (true) {
        Scanner input = new Scanner(System.in);
        try {
            String verified = input.nextLine();

            return verified;
        } catch (InputMismatchException e) {
            System.out.println("Ongeldige invoer. Probeer opnieuw.");

        }
    }
}

From source file:Main.java

public static String readTxtFile(String filePath) {
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        return null;
    }/*from   w  w w.ja va2s  .  c  om*/
    try {
        Scanner scanner = new Scanner(file);
        StringBuilder sb = new StringBuilder();
        while (scanner.hasNextLine()) {
            sb.append(scanner.nextLine());
        }
        scanner.close();
        return sb.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * This API compares if two files content is identical. It ignores extra
 * spaces and new lines while comparing/* www.  jav a2 s.co m*/
 *
 * @param sourceFile
 * @param destFile
 * @return
 * @throws Exception
 */
public static boolean isFilesIdentical(URL sourceFile, File destFile) throws Exception {
    try {
        Scanner sourceFileScanner = new Scanner(sourceFile.openStream());
        Scanner destFileScanner = new Scanner(destFile);

        while (sourceFileScanner.hasNext()) {
            /*
             * If source file is having next token then destination file
             * also should have next token, else they are not identical.
             */
            if (!destFileScanner.hasNext()) {
                destFileScanner.close();
                sourceFileScanner.close();
                return false;
            }
            if (!sourceFileScanner.next().equals(destFileScanner.next())) {
                sourceFileScanner.close();
                destFileScanner.close();
                return false;
            }
        }
        /*
         * Handling the case where source file is empty and destination file
         * is having text
         */
        if (destFileScanner.hasNext()) {
            destFileScanner.close();
            sourceFileScanner.close();
            return false;
        } else {
            destFileScanner.close();
            sourceFileScanner.close();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } /*finally {
      sourceFile.close();
      }*/
}

From source file:Main.java

/**
 * Returns a list of {@link Pair<String, String> NameValuePairs} as built from the
 * URI's query portion. For example, a URI of
 * http://example.org/path/to/file?a=1&b=2&c=3 would return a list of three
 * NameValuePairs, one for a=1, one for b=2, and one for c=3.
 * <p>//www .  j  ava 2  s.  c o m
 * This is typically useful while parsing an HTTP PUT.
 *
 * @param uri
 *            uri to parse
 * @param encoding
 *            encoding to use while parsing the query
 */
public static List<Pair<String, String>> parse(final URI uri, final String encoding) {
    List<Pair<String, String>> result = new ArrayList<Pair<String, String>>();
    final String query = uri.getRawQuery();
    if (query != null && query.length() > 0) {
        parse(result, new Scanner(query), encoding);
    }
    return result;
}

From source file:UtilFunctions.java

public static String[] parseFileTypes(String inputString) {

    //ArrayList to store each parsed filetype
    ArrayList<String> fileTypes = new ArrayList<>();

    //Scanner to iterate over the string filetype by filetype
    Scanner in = new Scanner(inputString);
    in.useDelimiter("[,] | [, ]");

    //Iterate over the string
    while (in.hasNext()) {
        fileTypes.add(in.next());/*  www  .  j  a v a  2 s  .c o  m*/
    }

    if (fileTypes.size() > 0) {
        //Generate an array from the ArrayList
        String[] returnArray = new String[fileTypes.size()];
        returnArray = fileTypes.toArray(returnArray);

        //Return that magnificent array
        return returnArray;

    } else {
        return null;
    }
}

From source file:IO.java

public static int[] readIntVec(File file, int nData) {
    int[] data = new int[nData];
    try {/*from w w  w.j  ava2 s .com*/
        Scanner sc = new Scanner(file);
        sc.useDelimiter(",|\\n");

        for (int i = 0; i < nData; i++) {
            data[i] = sc.nextInt();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return data;
}

From source file:edu.upenn.cis.FastAlign.java

/**
 * Prints alignments for options specified by command line arguments.
 * @param argv  parameters to be used by FastAlign.
 *//*from   ww w.  j  a v  a  2s  .  co  m*/
public static void main(String[] argv) {

    FastAlign align = FastAlign.initCommandLine(argv);
    if (align == null) {
        System.err.println("Usage: java " + FastAlign.class.getCanonicalName() + " -i file.fr-en\n"
                + " Standard options ([USE] = strongly recommended):\n" + "  -i: [REQ] Input parallel corpus\n"
                + "  -v: [USE] Use Dirichlet prior on lexical translation distributions\n"
                + "  -d: [USE] Favor alignment points close to the monotonic diagonoal\n"
                + "  -o: [USE] Optimize how close to the diagonal alignment points should be\n"
                + "  -r: Run alignment in reverse (condition on target and predict source)\n"
                + "  -c: Output conditional probability table\n"
                + "  -e: Start with existing conditional probability table\n" + " Advanced options:\n"
                + "  -I: number of iterations in EM training (default = 5)\n"
                + "  -p: p_null parameter (default = 0.08)\n" + "  -N: No null word\n"
                + "  -a: alpha parameter for optional Dirichlet prior (default = 0.01)\n"
                + "  -T: starting lambda for diagonal distance parameter (default = 4)\n");
        System.exit(1);
    }
    boolean use_null = !align.no_null_word;
    if (align.variational_bayes && align.alpha <= 0.0) {
        System.err.println("--alpha must be > 0\n");
        System.exit(1);
    }
    double prob_align_not_null = 1.0 - align.prob_align_null;
    final int kNULL = align.d.Convert("<eps>");
    TTable s2t = new TTable();
    if (!align.existing_probability_filename.isEmpty()) {
        boolean success = s2t.ImportFromFile(align.existing_probability_filename, '\t', align.d);
        if (!success) {
            System.err.println("Can't read table " + align.existing_probability_filename);
            System.exit(1);
        }
    }
    Map<Pair, Integer> size_counts = new HashMap<Pair, Integer>();
    double tot_len_ratio = 0;
    double mean_srclen_multiplier = 0;
    List<Double> probs = new ArrayList<Double>();
    ;
    // E-M Iterations Loop TODO move this into a method?
    for (int iter = 0; iter < align.iterations || (iter == 0 && align.iterations == 0); ++iter) {
        final boolean final_iteration = (iter >= (align.iterations - 1));
        System.err.println("ITERATION " + (iter + 1) + (final_iteration ? " (FINAL)" : ""));
        Scanner in = null;
        try {
            in = new Scanner(new File(align.input));
            if (!in.hasNextLine()) {
                System.err.println("Can't read " + align.input);
                System.exit(1);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("Can't read " + align.input);
            System.exit(1);
        }

        double likelihood = 0;
        double denom = 0.0;
        int lc = 0;
        boolean flag = false;
        String line;
        //         String ssrc, strg;
        ArrayList<Integer> src = new ArrayList<Integer>();
        ArrayList<Integer> trg = new ArrayList<Integer>();
        double c0 = 0;
        double emp_feat = 0;
        double toks = 0;
        // Iterate over each line of the input file
        while (in.hasNextLine()) {
            line = in.nextLine();
            ++lc;
            if (lc % 1000 == 0) {
                System.err.print('.');
                flag = true;
            }
            if (lc % 50000 == 0) {
                System.err.println(" [" + lc + "]\n");
                System.err.flush();
                flag = false;
            }
            src.clear();
            trg.clear(); // TODO this is redundant; src and tgt cleared in ParseLine
            // Integerize and split source and target lines.
            align.ParseLine(line, src, trg);
            if (align.is_reverse) {
                ArrayList<Integer> tmp = src;
                src = trg;
                trg = tmp;
            }
            // TODO Empty lines break the parser. Should this be true?
            if (src.size() == 0 || trg.size() == 0) {
                System.err.println("Error in line " + lc + "\n" + line);
                System.exit(1);
            }
            if (iter == 0) {
                tot_len_ratio += ((double) trg.size()) / ((double) src.size());
            }
            denom += trg.size();
            probs.clear();
            // Add to pair length counts only if first iteration.
            if (iter == 0) {
                Pair pair = new Pair(trg.size(), src.size());
                Integer value = size_counts.get(pair);
                if (value == null)
                    value = 0;
                size_counts.put(pair, value + 1);
            }
            boolean first_al = true; // used when printing alignments
            toks += trg.size();
            // Iterate through the English tokens
            for (int j = 0; j < trg.size(); ++j) {
                final int f_j = trg.get(j);
                double sum = 0;
                double prob_a_i = 1.0 / (src.size() + (use_null ? 1 : 0)); // uniform (model 1)
                if (use_null) {
                    if (align.favor_diagonal) {
                        prob_a_i = align.prob_align_null;
                    }
                    probs.add(0, s2t.prob(kNULL, f_j) * prob_a_i);
                    sum += probs.get(0);
                }
                double az = 0;
                if (align.favor_diagonal)
                    az = DiagonalAlignment.computeZ(j + 1, trg.size(), src.size(), align.diagonal_tension)
                            / prob_align_not_null;
                for (int i = 1; i <= src.size(); ++i) {
                    if (align.favor_diagonal)
                        prob_a_i = DiagonalAlignment.unnormalizedProb(j + 1, i, trg.size(), src.size(),
                                align.diagonal_tension) / az;
                    probs.add(i, s2t.prob(src.get(i - 1), f_j) * prob_a_i);
                    sum += probs.get(i);
                }
                if (final_iteration) {
                    double max_p = -1;
                    int max_index = -1;
                    if (use_null) {
                        max_index = 0;
                        max_p = probs.get(0);
                    }
                    for (int i = 1; i <= src.size(); ++i) {
                        if (probs.get(i) > max_p) {
                            max_index = i;
                            max_p = probs.get(i);
                        }
                    }
                    if (max_index > 0) {
                        if (first_al)
                            first_al = false;
                        else
                            System.out.print(' ');
                        if (align.is_reverse)
                            System.out.print("" + j + '-' + (max_index - 1));
                        else
                            System.out.print("" + (max_index - 1) + '-' + j);
                    }
                } else {
                    if (use_null) {
                        double count = probs.get(0) / sum;
                        c0 += count;
                        s2t.Increment(kNULL, f_j, count);
                    }
                    for (int i = 1; i <= src.size(); ++i) {
                        final double p = probs.get(i) / sum;
                        s2t.Increment(src.get(i - 1), f_j, p);
                        emp_feat += DiagonalAlignment.feature(j, i, trg.size(), src.size()) * p;
                    }
                }
                likelihood += Math.log(sum);
            }
            if (final_iteration)
                System.out.println();
        }

        // log(e) = 1.0
        double base2_likelihood = likelihood / Math.log(2);

        if (flag) {
            System.err.println();
        }
        if (iter == 0) {
            mean_srclen_multiplier = tot_len_ratio / lc;
            System.err.println("expected target length = source length * " + mean_srclen_multiplier);
        }
        emp_feat /= toks;
        System.err.println("  log_e likelihood: " + likelihood);
        System.err.println("  log_2 likelihood: " + base2_likelihood);
        System.err.println("     cross entropy: " + (-base2_likelihood / denom));
        System.err.println("        perplexity: " + Math.pow(2.0, -base2_likelihood / denom));
        System.err.println("      posterior p0: " + c0 / toks);
        System.err.println(" posterior al-feat: " + emp_feat);
        //System.err.println("     model tension: " + mod_feat / toks );
        System.err.println("       size counts: " + size_counts.size());
        if (!final_iteration) {
            if (align.favor_diagonal && align.optimize_tension && iter > 0) {
                for (int ii = 0; ii < 8; ++ii) {
                    double mod_feat = 0;
                    Iterator<Map.Entry<Pair, Integer>> it = size_counts.entrySet().iterator();
                    for (; it.hasNext();) {
                        Map.Entry<Pair, Integer> entry = it.next();
                        final Pair p = entry.getKey();
                        for (int j = 1; j <= p.first; ++j)
                            mod_feat += entry.getValue() * DiagonalAlignment.computeDLogZ(j, p.first, p.second,
                                    align.diagonal_tension);
                    }
                    mod_feat /= toks;
                    System.err.println("  " + ii + 1 + "  model al-feat: " + mod_feat + " (tension="
                            + align.diagonal_tension + ")");
                    align.diagonal_tension += (emp_feat - mod_feat) * 20.0;
                    if (align.diagonal_tension <= 0.1)
                        align.diagonal_tension = 0.1;
                    if (align.diagonal_tension > 14)
                        align.diagonal_tension = 14;
                }
                System.err.println("     final tension: " + align.diagonal_tension);
            }
            if (align.variational_bayes)
                s2t.NormalizeVB(align.alpha);
            else
                s2t.Normalize();
            //prob_align_null *= 0.8; // XXX
            //prob_align_null += (c0 / toks) * 0.2;
            prob_align_not_null = 1.0 - align.prob_align_null;
        }

    }
    if (!align.conditional_probability_filename.isEmpty()) {
        System.err.println("conditional probabilities: " + align.conditional_probability_filename);
        s2t.ExportToFile(align.conditional_probability_filename, align.d);
    }
    System.exit(0);
}