Example usage for java.util Scanner hasNext

List of usage examples for java.util Scanner hasNext

Introduction

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

Prototype

public boolean hasNext() 

Source Link

Document

Returns true if this scanner has another token in its input.

Usage

From source file:com.pushinginertia.commons.net.client.AbstractHttpPostClient.java

/**
 * Retrieves the response message from the remote host.
 *
 * @param con instantiated connection/*w w w.j a  v a2 s.c om*/
 * @param encoding encoding to use in the response
 * @return response message from the remote host or null if none exists
 * @throws HttpConnectException if there is a problem retrieving the message
 */
protected String getResponseMessage(final HttpURLConnection con, final String encoding)
        throws HttpConnectException {
    try {
        final InputStream is = con.getInputStream();
        final Scanner s = new Scanner(is, encoding);
        s.useDelimiter("\\A"); // \A is the beginning of input
        if (s.hasNext())
            return s.next();
        return null;
    } catch (NoSuchElementException e) {
        // no input
        return null;
    } catch (Exception e) {
        final String msg = "An unexpected error occurred while trying to retrieve the response message from ["
                + getUrl() + "]: " + e.getMessage();
        LOG.error(getClass().getSimpleName(), msg, e);
        throw new HttpConnectException(msg, e);
    }
}

From source file:org.cyclop.service.importer.intern.ParallelQueryImporter.java

private ImmutableList<CqlQuery> parse(Scanner scanner) {
    StopWatch timer = null;//from  w  ww .  ja  va 2 s. c  om
    if (LOG.isDebugEnabled()) {
        timer = new StopWatch();
        timer.start();
    }

    ImmutableList.Builder<CqlQuery> build = ImmutableList.builder();
    while (scanner.hasNext()) {
        String nextStr = StringUtils.trimToNull(scanner.next());
        if (nextStr == null) {
            continue;
        }
        CqlQuery query = new CqlQuery(CqlQueryType.UNKNOWN, nextStr);
        build.add(query);
    }
    ImmutableList<CqlQuery> res = build.build();

    if (LOG.isDebugEnabled()) {
        timer.stop();
        LOG.debug("Parsed {} queries in {}", res.size(), timer.toString());
    }
    return res;
}

From source file:org.dbpedia.spotlight.io.DatasetSplitter.java

public void run(InputStream stream) throws IOException {
    String currentItem = "";
    List<String> items = new ArrayList<String>();
    Scanner scanner = new Scanner(new InputStreamReader(stream, "UTF-8"));
    int nItemsKept = 0;
    while (scanner.hasNext()) {
        String line = scanner.nextLine();
        incrementalId++;//from  w w  w . j  a v  a  2  s  . com

        if (line == null || line.trim().equals(""))
            continue;

        String[] fields = line.split("\t");
        String uri;
        if (fields.length >= 5) {
            uri = fields[0];
        } else {
            uri = fields[1];
        }
        //                    String surfaceForm = fields[1];
        //                    String context = fields[2];
        //                    String offset = fields[3];
        //                    String type = fields[4];

        //Tuple5<String,String,String,String,String> t = new Tuple5<String,String,String,String,String>(surfaceForm, uri, context, offset, type);

        if (!uri.equals(currentItem)) {

            if (shouldKeepTheseOccurrences(items)) {
                nItemsKept++;
                LOG.trace("End of current item: " + currentItem + " / size: " + items.size() + " - saving!");
                split(items);
            } // else ignore
              //reset current item
            currentItem = uri;
            items = new ArrayList<String>();
        }
        items.add(line.toString());

        if (incrementalId % 50000 == 0)
            LOG.info("Processed " + incrementalId + " occurrences. Kept occurrences for " + nItemsKept
                    + " URIs.");
    }
    scanner.close();
    LOG.info("Processed " + incrementalId + " occurrences. Kept occurrences for " + nItemsKept + " URIs");
}

From source file:eu.project.ttc.resources.DictionaryResource.java

private Set<Entry<String, String>> parse(InputStream inputStream) throws IOException {
    Scanner scanner = null;
    try {/*w ww.j a  va  2  s . c  o m*/
        Set<Entry<String, String>> entries = new HashSet<Entry<String, String>>();
        scanner = new Scanner(inputStream, "UTF-8");
        scanner.useDelimiter("\\r?\\n");
        while (scanner.hasNext()) {
            String line = scanner.next();
            String[] items = line.split("\t");
            String source = items[0];
            if (source != null) {
                String target = items[1];
                if (target != null) {
                    Entry<String, String> entry = new SimpleEntry<String, String>(source, target);
                    entries.add(entry);
                }
            }
        }
        return entries;
    } finally {
        IOUtils.closeQuietly(scanner);
    }
}

From source file:DynamiskDemo2.java

/**
 * Constructs a new demonstration application.
 *
 * @param title  the frame title.//from w w  w  . j  a va  2  s  . c  o m
 */
public DynamiskDemo2(final String title) {

    super(title);
    this.series = new XYSeries(title, false, false);
    final XYSeriesCollection dataset = new XYSeriesCollection(this.series);
    final JFreeChart chart = createChart(dataset);

    final ChartPanel chartPanel = new ChartPanel(chart);
    final JButton button = new JButton("Add New Data Item");
    button.setActionCommand("ADD_DATA");
    button.addActionListener(this);

    final JPanel content = new JPanel(new BorderLayout());
    content.add(chartPanel);
    content.add(button, BorderLayout.SOUTH);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(content);
    String fil = "C:" + File.separator + "Users" + File.separator + "madso" + File.separator + "Documents"
            + File.separator + "!Privat" + File.separator + "DTU 2016-2020" + File.separator + "MATLAB";
    String filnavn = "EKGdata";
    try {
        Scanner sc = new Scanner(new FileReader(fil + File.separator + filnavn));

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                if (sc.hasNext()) {
                    final double newItem = Double.parseDouble(sc.next());
                    series.add(x, newItem);
                    x += 10;
                }
            }

        }, 100, 2);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:eu.project.ttc.resources.GeneralLanguageResource.java

public void load(InputStream inputStream) throws ResourceInitializationException {
    words = Sets.newHashSet();/*w  w  w .jav  a 2 s. co  m*/
    Scanner scanner = null;
    try {
        scanner = new Scanner(inputStream, "UTF-8");
        scanner.useDelimiter("\n");
        int index = 0;
        while (scanner.hasNext()) {
            index++;
            String line = scanner.next();
            String[] items = line.split("::");
            if (items.length == 3) {
                String key = items[0].trim();
                if (!key.contains(" "))
                    this.words.add(key);
                Integer value = Integer.valueOf(items[2].trim());
                this.cumulatedFrequency += value.intValue();
                String lemma = key;
                this.frequencies.put(lemma, new Entry(lemma, items[1], new Integer(value.intValue())));
            } else {
                throw new IOException("Wrong general language format at line " + index + ": " + line);
            }
        }
        this.words = ImmutableSet.copyOf(this.words);

        if (this.frequencies.containsKey(PARAM_NB_CORPUS_WORDS))
            this.nbCorpusWords = this.frequencies.get(PARAM_NB_CORPUS_WORDS).iterator().next().getFrequency();
        else {
            LOGGER.warn("No such key for in GeneralLanguage resource {}", PARAM_NB_CORPUS_WORDS);
            LOGGER.warn("Switch to cumulatedFrequency mode");
            this.cumulatedFrequencyMode = true;
        }
    } catch (Exception e) {
        throw new ResourceInitializationException(e);
    } finally {
        IOUtils.closeQuietly(scanner);
    }
}

From source file:edu.harvard.iq.dvn.ingest.dsb.impl.DvnJavaFieldCutter.java

public void subsetFile(InputStream in, String outfile, Set<Integer> columns, Long numCases, String delimiter) {
    try {/* w w  w . j  av  a2  s. c  om*/
        Scanner scanner = new Scanner(in);

        dbgLog.fine("outfile=" + outfile);

        BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
        scanner.useDelimiter("\\n");

        for (long caseIndex = 0; caseIndex < numCases; caseIndex++) {
            if (scanner.hasNext()) {
                String[] line = (scanner.next()).split(delimiter, -1);
                List<String> ln = new ArrayList<String>();
                for (Integer i : columns) {
                    ln.add(line[i]);
                }
                out.write(StringUtils.join(ln, "\t") + "\n");
            } else {
                throw new RuntimeException("Tab file has fewer rows than the determined number of cases.");
            }
        }

        while (scanner.hasNext()) {
            if (!"".equals(scanner.next())) {
                throw new RuntimeException(
                        "Tab file has extra nonempty rows than the determined number of cases.");

            }
        }

        scanner.close();
        out.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:eu.project.ttc.resources.PrefixTree.java

@Override
public void load(DataResource data) throws ResourceInitializationException {
    InputStream inputStream = null;
    try {// ww  w.j  a  va  2s  . c om
        inputStream = data.getInputStream();
        Scanner scanner = null;
        try {
            String line;
            scanner = new Scanner(inputStream, "UTF-8");
            scanner.useDelimiter(TermSuiteConstants.LINE_BREAK);
            while (scanner.hasNext()) {
                line = scanner.next().split(TermSuiteConstants.DIESE)[0].trim();
                if (line.startsWith("#"))
                    continue;
                else if (line.isEmpty())
                    continue;
                else if (line.endsWith("-")) {
                    if (line.length() == 1)
                        continue;
                    else
                        line = line.substring(0, line.length() - 1);
                }

                rootNode.indexString(toCharQueue(line));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ResourceInitializationException(e);
        } finally {
            IOUtils.closeQuietly(scanner);
        }
    } catch (IOException e) {
        LOGGER.error("Could not load file {}", data.getUrl());
        throw new ResourceInitializationException(e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

}

From source file:eu.project.ttc.resources.ReferenceTermList.java

@Override
public void load(DataResource data) throws ResourceInitializationException {
    InputStream inputStream = null;
    try {/* ww w. j ava  2s .  c  o m*/
        this.path = data.getUri().toString();
        LOGGER.debug("Loading reference term list at {}", this.path);
        inputStream = data.getInputStream();
        Scanner scanner = null;
        try {
            scanner = new Scanner(inputStream);
            scanner.useDelimiter(TermSuiteConstants.LINE_BREAK);
            while (scanner.hasNext()) {
                lineNb++;
                String line = scanner.next().split(TermSuiteConstants.DIESE)[0].trim();
                List<String> els = Splitter.on(TermSuiteConstants.TAB).splitToList(line.trim().toLowerCase());
                if (!els.isEmpty()) {
                    if (els.size() != 3)
                        LOGGER.warn("Ignoring line {} : should have exactly 3 elements ({})", lineNb, line);
                    else {
                        int id = Integer.parseInt(els.get(0));
                        RTLTerm refTerm = new RTLTerm(lineNb, id, els.get(2),
                                els.get(1).toLowerCase().equals("v"));
                        if (refTerm.isVariant()) {
                            if (!bases.containsKey(id)) {
                                LOGGER.warn("No such base term id {} for variant term {}", id, refTerm);
                                continue;
                            } else
                                bases.get(id).addVariant(refTerm);
                        } else {
                            bases.put(id, refTerm);
                        }
                    }
                }
            }
            this.bases = ImmutableMap.copyOf(this.bases);
            int total = 0;
            for (RTLTerm ref : bases.values())
                total += 1 + ref.getVariants().size();
            LOGGER.debug("Reference term list loaded (nb terms: {}, nb terms and variants: {})",
                    this.bases.keySet().size(), total);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ResourceInitializationException(e);
        } finally {
            IOUtils.closeQuietly(scanner);
        }
    } catch (IOException e) {
        LOGGER.error("Could not load file {}", data.getUrl());
        throw new ResourceInitializationException(e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:ddf.catalog.transformer.csv.common.CsvTransformerTest.java

private void validate(Scanner scanner, String[] expectedValues) {
    for (String expectedValue : expectedValues) {
        assertThat("Expected next value to be " + expectedValue + " but scanner.hasNext() returned false",
                scanner.hasNext(), is(true));
        assertThat(scanner.next(), is(expectedValue));
    }/*from w w w  .j a va2  s . c  o  m*/
}