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

public KnowledgeBooksNlpGenerateRdfPropertiesFromWebPages(String config_file_path, PrintWriter out)
        throws IOException {
    this.out = out;
    extractNames = new ExtractNames();
    autoTagger = new AutoTagger();
    List<String> lines = (List<String>) FileUtils.readLines(new File(config_file_path));
    for (String line : lines) {
        Scanner scanner = new Scanner(line);
        scanner.useDelimiter(" ");
        try {/*from w ww  .  j a va2  s . c  o m*/
            String starting_url = scanner.next();
            int spider_depth = Integer.parseInt(scanner.next());
            spider(starting_url, spider_depth);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    this.out.close();
}

From source file:DataSci.main.JsonRequestResponseController.java

/**
 * Read the JSON schema from the file rrsJson.json
 * /*w ww. j  ava 2s. co m*/
 * @param filename It expects a fully qualified file name that contains input JSON file
 */
public void readJson(String filename) {
    try {
        File apiFile = new File(filename);
        Scanner sc = new Scanner(apiFile);
        jsonBody = "";
        while (sc.hasNext()) {
            jsonBody += sc.nextLine() + "\n";
        }
        sc.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static final String readFully(final InputStream pInputStream) throws IOException {
    final StringBuilder sb = new StringBuilder();
    final Scanner sc = new Scanner(pInputStream);
    while (sc.hasNextLine()) {
        sb.append(sc.nextLine());// ww w  .  j av  a 2 s.  c  o m
    }
    return sb.toString();
}

From source file:com.deployd.Deployd.java

public static JSONObject delete(String uri) throws ClientProtocolException, IOException, JSONException {

    HttpDelete post = new HttpDelete(endpoint + uri);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(post);
    ByteArrayEntity e = (ByteArrayEntity) response.getEntity();
    InputStream is = e.getContent();
    String data = new Scanner(is).next();
    JSONObject result = new JSONObject(data);
    return result;
}

From source file:org.knoxcraft.http.client.ClientMultipartFormPost.java

private static String readFromFile(File file) throws IOException {
    StringBuilder buf = new StringBuilder();
    Scanner scan = new Scanner(new FileInputStream(file));
    while (scan.hasNextLine()) {
        String line = scan.nextLine();
        buf.append(line);//from w  w  w . j  a  v  a2  s .c o  m
        buf.append("\n");
    }
    scan.close();
    return buf.toString();
}

From source file:EntityToD2RHelpers.java

public EntityToD2RHelpers(String uri, String config_file, List<String> people, List<String> places,
        PrintWriter out) throws Exception {
    // In this example, I am assuming that the D2R server is running on localhost:2020
    //out.println("PREFIX vocab: <http://localhost:2020/vocab/resource/>");
    List<String> lines = (List<String>) FileUtils.readLines(new File(config_file));
    String d2r_host_and_port = lines.remove(0);
    String[] info = d2r_host_and_port.split(" ");
    System.out.println("D2R host = |" + info[0] + "| and port = |" + info[1] + "|");
    for (String line : lines) {
        Scanner scanner = new Scanner(line);
        scanner.useDelimiter(" ");
        String d2r_type = scanner.next();
        System.out.println("* d2r_type = " + d2r_type);
        while (scanner.hasNext()) {
            String term = scanner.next();
            String[] property_and_entity_type = term.split("/");
            System.out.println("   property: " + property_and_entity_type[0] + " entity type: "
                    + property_and_entity_type[1]);

            if (property_and_entity_type[1].equals("person")) {
                for (String person : people) {
                    // perform SPARQL queries to D2R server:
                    String sparql = "PREFIX vocab: <http://localhost:2020/vocab/resource/>\n"
                            + "SELECT ?subject ?name WHERE {\n" + "     ?subject " + property_and_entity_type[0]
                            + " ?name \n" + " FILTER regex(?name, \"" + person + "\") .\n" + "}\n"
                            + "LIMIT 10\n";
                    SparqlClient test = new SparqlClient("http://localhost:2020/sparql", sparql);
                    for (Map<String, String> bindings : test.variableBindings()) {
                        System.out.print("D2R result:" + bindings);
                        if (bindings.keySet().size() > 0) {
                            String blank_node = blankNodeURI("person");
                            out.println(blank_node + " <http://knowledgebooks.com/rdf/personName> \""
                                    + person.replaceAll("\"", "'") + "\" .");
                            out.println("<" + uri + "> <http://knowledgebooks.com/rdf/containsPerson> "
                                    + blank_node + " .");
                            out.println(blank_node + " <http://knowledgebooks.com/rdf/d2r_uri> \""
                                    + bindings.get("subject") + "\" .");
                        }/*w w w .  jav  a2s .co m*/
                    }
                }
            } else if (property_and_entity_type[1].equals("place")) {
                for (String place : places) {
                    // perform SPARQL queries to D2R server:
                    String sparql = "PREFIX vocab: <http://localhost:2020/vocab/resource/>\n"
                            + "SELECT ?subject ?name WHERE {\n" + "     ?subject " + property_and_entity_type[0]
                            + " ?name \n" + " FILTER regex(?name, \"" + place + "\") .\n" + "}\n"
                            + "LIMIT 10\n";
                    SparqlClient test = new SparqlClient("http://localhost:2020/sparql", sparql);
                    for (Map<String, String> bindings : test.variableBindings()) {
                        System.out.print("D2R result:" + bindings);
                        if (bindings.keySet().size() > 0) {
                            String blank_node = blankNodeURI("place");
                            out.println(blank_node + " <http://knowledgebooks.com/rdf/placeName> \""
                                    + place.replaceAll("\"", "'") + "\" .");
                            out.println("<" + uri + "> <http://knowledgebooks.com/rdf/containsPlace> "
                                    + blank_node + " .");
                            out.println(blank_node + " <http://knowledgebooks.com/rdf/d2r_uri> \""
                                    + bindings.get("subject") + "\" .");
                        }
                    }
                }
            }
        }
    }
    out.close();
}

From source file:formatMessage.VerifyInputScanner.java

public static double verifyDouble() {

    while (true) {
        Scanner input = new Scanner(System.in);
        try {/*from   w  w w . j a  va 2 s. com*/
            Double verified = input.nextDouble();

            return verified;
        } catch (InputMismatchException e) {
            System.out.println("Geen geldig nummer. Probeer opnieuw.");

        }
    }
}

From source file:Questao3.java

public Questao3() throws IOException {
    this.in = new Scanner(System.in);
    this.arquivos = new String[MAXFILES];
    this.images = new ArrayList<>();
    for (int i = 0; i < MAXFILES; i++) {
        arquivos[i] = "(" + (i + 1) + ").jpg";
    }/* w  w  w.j ava 2  s  .  c om*/

    desvioPadrao();
    criaGrafico();
    media();

    return;
}

From source file:javaapplication1.Prog.java

public void run(String[] args) throws JSONException, IOException {
    int k = 0;//from   w ww  . j  av a 2 s.  c  om
    while (k == 0) {
        Scanner sc = new Scanner(System.in);
        System.out.print("your command:");
        String i = sc.nextLine();

        if ("exit".equals(i))
            k = 1;
        else {
            if ("help".equals(i)) {
                System.out.print(help);
            } else if ("transfer".equals(i)) {
                System.out.print("user:");
                String user = sc.nextLine();
                System.out.print("from account:");
                String account1 = sc.nextLine();
                System.out.print("to account:");
                String account2 = sc.nextLine();
                System.out.print("amount:");
                Long amount = sc.nextLong();
                Long amount1 = getAmountfromAcc(account1, user);
                Long amount2 = getAmountfromAcc(account2, user);
                JSONObject obj1 = new JSONObject();
                JSONObject obj2 = new JSONObject();
                obj1.put("name", account1);
                obj1.put("amount", amount1 - amount);
                String obj11 = obj1.toString();
                transferStuff(obj11, user);

                obj2.put("name", account2);
                obj2.put("amount", amount2 + amount);
                String obj22 = obj2.toString();
                transferStuff(obj22, user);
                System.out.print("accounts updated\n");

            } else
                System.out.print("incorrect command\n");
        }
    }
}

From source file:controller.Parser.java

static LP parse(File f) throws FileNotFoundException {
    Scanner s = new Scanner(f);
    Pattern p = Pattern.compile(dvarreg);

    HashMap<String, Integer> x = new HashMap<String, Integer>();
    HashMap<Integer, String> xReverse = new HashMap<Integer, String>();
    int xcol = 0;

    /* Get input size and names of the decision variables. */
    int constraints = -1; // Take the objective function into account.
    while (s.hasNextLine()) {
        String line = s.nextLine();

        if (line.trim().equals(""))
            continue;

        /* //from w w w  .  j  av a  2  s  .co m
         * TODO: Beware, will now accept invalid
         * files with multiple objective functions.
         */
        /*            if (!validConstraint(line) && !validObj(line)) {
        String e = "Unsupported format in file " + f;
        throw new IllegalArgumentException(e);
                    } */

        Matcher m = p.matcher(line);

        while (m.find()) {
            String var = m.group(3);
            if (validVarName(var) && !x.containsKey(var)) {
                x.put(var, xcol);
                xReverse.put(xcol++, var);
            }
        }
        constraints++;
    }

    BigFraction[][] Ndata = new BigFraction[constraints][x.size()];
    for (int i = 0; i < Ndata.length; i++) {
        Arrays.fill(Ndata[i], BigFraction.ZERO);
    }
    BigFraction[] bdata = new BigFraction[constraints];
    BigFraction[] cdata = new BigFraction[x.size()];
    Arrays.fill(cdata, BigFraction.ZERO);

    s = new Scanner(f);

    String obj = s.nextLine();
    Matcher m = p.matcher(obj);

    while (m.find()) {
        String var = m.group(3);
        if (!x.containsKey(var))
            continue;

        String sign = m.group(1);
        if (sign == null)
            sign = "+";

        String coeffStr = m.group(2);
        BigFraction coeff;
        if (coeffStr == null) {
            coeff = BigFraction.ONE;
        } else {
            coeff = new BigFraction(Double.parseDouble(coeffStr));
        }
        if (sign.equals("-"))
            coeff = coeff.negate();

        cdata[x.get(var)] = coeff;
    }

    int row = 0;
    while (s.hasNextLine()) {
        String line = s.nextLine();
        String[] split = line.split("<=");
        if (line.trim().equals(""))
            continue;
        if (split.length != 2) {
            String e = "Unsupported format in file " + f;
            throw new IllegalArgumentException(e);
        }
        m = p.matcher(line);
        bdata[row] = new BigFraction(Double.parseDouble(split[1]));

        while (m.find()) {
            String var = m.group(3);
            if (!x.containsKey(var))
                continue;

            String sign = m.group(1);
            if (sign == null)
                sign = "+";

            String coeffStr = m.group(2);
            BigFraction coeff;
            if (coeffStr == null) {
                coeff = BigFraction.ONE;
            } else {
                coeff = new BigFraction(Double.parseDouble(coeffStr));
            }
            if (sign.equals("-"))
                coeff = coeff.negate();

            Ndata[row][x.get(var)] = coeff;
        }
        row++;
    }

    return new LP(new Array2DRowFieldMatrix<BigFraction>(Ndata), new ArrayFieldVector<BigFraction>(bdata),
            new ArrayFieldVector<BigFraction>(cdata), xReverse);
}