Java Scanner Usage listify(String s, String prefix, String... ignorelist)

Here you can find the source of listify(String s, String prefix, String... ignorelist)

Description

Parses a String, assumed to be a list, into a String array.

License

Open Source License

Parameter

Parameter Description
s The String to use (must be delimited by new line chars)
prefix Adds a prefix to the beginning of each element. Specify "" for no prefix.
ignorelist If an item in the list contains a String in this list, it won't be included in the final result. Whatever you do, don't put an empty string in here; you'll be sorry :3

Declaration


public static String[] listify(String s, String prefix, String... ignorelist) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    /**/*from w  w w.ja v a2s  .c o m*/
     * Parses a String, assumed to be a list, into a String array. Each item in the list must be
     * separated by a newline character. Space characters are ignored. The ignore list is case
     * sensitive, and only checks if each String in the list starts with the specified text; this is
     * useful for parsing out comments and/or unwanted crap.
     * 
     * @param s The String to use (must be delimited by new line chars)
     * @param prefix Adds a prefix to the beginning of each element. Specify "" for no prefix.
     * @param ignorelist If an item in the list contains a String in this list, it won't be included
     *           in the final result. Whatever you do, don't put an empty string in here; you'll be
     *           sorry :3
     */

    public static String[] listify(String s, String prefix, String... ignorelist) {
        ArrayList<String> l = new ArrayList<String>();
        Scanner m = new Scanner(s);
        while (m.hasNextLine()) {
            String b = m.nextLine().trim();
            if (b.length() > 0)
                l.add(prefix + b);
        }

        if (ignorelist.length > 0) {
            ArrayList<String> x = new ArrayList<String>();

            for (String a : l) {
                boolean good = true;

                for (String bad : ignorelist)
                    if (a.contains(bad))
                        good = false;

                if (good)
                    x.add(a);
            }

            l = x;
        }

        return l.toArray(new String[0]);
    }
}

Related

  1. iterate(final String node)
  2. joinSqlStatements(Collection scripts)
  3. leerOpcion(String opciones, String msgUsr, String msgErr)
  4. leerTexto(String msgUsr)
  5. listify(String s, String prefix, String... ignorelist)
  6. loadElements(Scanner scan, int elementCount, int skipCount)
  7. loadStringDoubleMap(Scanner scanner)
  8. menu()
  9. nextChar(Scanner s)