Example usage for java.util Scanner hasNextLong

List of usage examples for java.util Scanner hasNextLong

Introduction

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

Prototype

public boolean hasNextLong() 

Source Link

Document

Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the #nextLong method.

Usage

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0 true 9000000000000000000000L";

    Scanner scanner = new Scanner(s);

    while (scanner.hasNext()) {
        System.out.println("Not Found :" + scanner.next());
        if (scanner.hasNextLong()) {
            System.out.println("Found :" + scanner.nextLong(20));
        }/* ww  w .  j a  v a 2s.co m*/
    }
    scanner.close();
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0 true ";
    Long l = 123456789098765432L;
    s = s + l;//from   w  w  w .j a  v  a  2s  .co m

    Scanner scanner = new Scanner(s);

    while (scanner.hasNext()) {

        System.out.println("Not Found :" + scanner.next());

        if (scanner.hasNextLong()) {
            System.out.println("Found :" + scanner.nextLong());
        }
    }
    scanner.close();
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com ";
    Long l = 1234567890987654L;/*from   w w  w.  j  a  va2s.co m*/
    s = s + l;

    Scanner scanner = new Scanner(s);

    // use US locale in order to be able to identify long in a string
    scanner.useLocale(Locale.US);

    while (scanner.hasNext()) {
        // check if the scanner's next token is along
        System.out.println(scanner.hasNextLong());

        System.out.println(scanner.next());
    }
    scanner.close();
}

From source file:com.flexive.shared.value.FxSelectMany.java

/**
 * Evaluates the given string value to an object of type SelectMany.
 *
 * @param value comma seperated list of selected entries
 * @return the value interpreted as SelectMany
 *///from  w  ww.  ja  v  a  2  s . c o  m
@Override
public SelectMany fromString(String value) {
    if (StringUtils.isEmpty(value))
        throw new FxInvalidParameterException("value", "ex.content.value.invalid.list").asRuntimeException();
    final List<FxSelectListItem> items = new ArrayList<FxSelectListItem>(10);
    final Scanner sc = new Scanner(value).useDelimiter(",");
    final FxEnvironment environment = CacheAdmin.getEnvironment();
    while (sc.hasNextLong()) {
        final long id = sc.nextLong();
        items.add(list != null ? list.getItem(id) : environment.getSelectListItem(id));
    }
    if (items.size() == 0)
        throw new FxInvalidParameterException("value", "ex.content.value.invalid.list").asRuntimeException();
    SelectMany sel = new SelectMany(items.get(0).getList());
    for (FxSelectListItem item : items)
        sel.selectItem(item);
    return sel;
}

From source file:org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.BlockPoolSlice.java

/**
 * Read in the cached DU value and return it if it is less than 600 seconds
 * old (DU update interval). Slight imprecision of dfsUsed is not critical
 * and skipping DU can significantly shorten the startup time.
 * If the cached value is not available or too old, -1 is returned.
 *//*from  ww w . ja  v  a2  s . c o m*/
long loadDfsUsed() {
    long cachedDfsUsed;
    long mtime;
    Scanner sc;

    try {
        sc = new Scanner(new File(currentDir, DU_CACHE_FILE), "UTF-8");
    } catch (FileNotFoundException fnfe) {
        return -1;
    }

    try {
        // Get the recorded dfsUsed from the file.
        if (sc.hasNextLong()) {
            cachedDfsUsed = sc.nextLong();
        } else {
            return -1;
        }
        // Get the recorded mtime from the file.
        if (sc.hasNextLong()) {
            mtime = sc.nextLong();
        } else {
            return -1;
        }

        // Return the cached value if mtime is okay.
        if (mtime > 0 && (Time.now() - mtime < 600000L)) {
            FsDatasetImpl.LOG.info("Cached dfsUsed found for " + currentDir + ": " + cachedDfsUsed);
            return cachedDfsUsed;
        }
        return -1;
    } finally {
        sc.close();
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.BlockPoolSlice.java

private void addReplicaToReplicasMap(Block block, ReplicaMap volumeMap, boolean isFinalized)
        throws IOException {
    ReplicaInfo newReplica = null;//from w w w .java 2  s. c o m
    long blockId = block.getBlockId();
    long genStamp = block.getGenerationStamp();
    if (isFinalized) {
        newReplica = new FinalizedReplica(blockId, block.getNumBytes(), genStamp, volume,
                DatanodeUtil.idToBlockDir(finalizedDir, blockId));
    } else {
        File file = new File(rbwDir, block.getBlockName());
        boolean loadRwr = true;
        File restartMeta = new File(file.getParent() + File.pathSeparator + "." + file.getName() + ".restart");
        Scanner sc = null;
        try {
            sc = new Scanner(restartMeta, "UTF-8");
            // The restart meta file exists
            if (sc.hasNextLong() && (sc.nextLong() > Time.now())) {
                // It didn't expire. Load the replica as a RBW.
                // We don't know the expected block length, so just use 0
                // and don't reserve any more space for writes.
                newReplica = new ReplicaBeingWritten(blockId, validateIntegrityAndSetLength(file, genStamp),
                        genStamp, volume, file.getParentFile(), null, 0);
                loadRwr = false;
            }
            sc.close();
            if (!restartMeta.delete()) {
                FsDatasetImpl.LOG.warn("Failed to delete restart meta file: " + restartMeta.getPath());
            }
        } catch (FileNotFoundException fnfe) {
            // nothing to do hereFile dir =
        } finally {
            if (sc != null) {
                sc.close();
            }
        }
        // Restart meta doesn't exist or expired.
        if (loadRwr) {
            newReplica = new ReplicaWaitingToBeRecovered(blockId, validateIntegrityAndSetLength(file, genStamp),
                    genStamp, volume, file.getParentFile());
        }
    }

    ReplicaInfo oldReplica = volumeMap.get(bpid, newReplica.getBlockId());
    if (oldReplica == null) {
        volumeMap.add(bpid, newReplica);
    } else {
        FsDatasetImpl.LOG.warn("Two block files with the same block id exist " + "on disk: "
                + oldReplica.getBlockFile() + " and " + newReplica.getBlockFile());
    }
}

From source file:org.eurekastreams.server.service.actions.strategies.activity.IterpolationListColliderTest.java

/**
 * Read an array of longs from a file.// www.  ja  v a  2s. co m
 * 
 * @param file
 *            the path to the file.
 * @param expectedSize
 *            the expected size of the list.
 * @return the array of longs.
 * @throws FileNotFoundException
 *             thrown if file can't be found.
 */
private static Long[] fileToList(final String file, final int expectedSize) throws FileNotFoundException {
    Scanner scanFile = new Scanner(new File(file));

    List<Long> list = new ArrayList<Long>();
    Scanner s = new Scanner(scanFile.nextLine());

    while (s.hasNextLong()) {
        list.add(s.nextLong());
    }

    Long[] arr = new Long[list.size()];

    list.toArray(arr);

    Assert.assertEquals(expectedSize, arr.length);

    return arr;
}

From source file:org.romaframework.aspect.view.html.RequestParserImpl.java

private void bindValues(final Map<String, Map<String, Object>> reqParams) {
    final HtmlViewSession session = HtmlViewAspectHelper.getHtmlViewSession();
    for (final String groupName : reqParams.keySet()) {
        if (!(TransformerHelper.POJO_ACTION_PREFIX.equals(groupName)
                || TransformerHelper.POJO_EVENT_PREFIX.equals(groupName))) {
            Scanner s = new Scanner(groupName);
            if (s.hasNextLong()) {
                final Long fieldId = s.nextLong();
                final HtmlViewRenderable renderable = session.getRenderableById(fieldId);
                if (renderable != null) {
                    final HtmlViewBinder binder = renderable.getTransformer().getBinder(renderable);
                    if (binder instanceof ViewComponent) {
                        Roma.view().getScreen().setActiveArea(((ViewComponent) binder).getScreenArea());
                    }/*from  w w  w.  ja  v  a2  s .  co  m*/
                    if (binder != null) {
                        // TODO handle BindingException
                        binder.bind(renderable, reqParams.get(groupName));
                    }
                }
            }
        }
    }
}

From source file:org.talend.dataprep.schema.csv.CSVFastHeaderAndTypeAnalyzer.java

/**
 * Performs type analysis for fields of the record at the specified index.
 * /*from   ww w. j av  a  2 s . c  o  m*/
 * @param i the index of the record to be analyzed.
 * @return the list of types of the record
 */
private List<Integer> setFieldType(int i) {
    List<Integer> result = new ArrayList<>();
    String line = i < sampleLines.size() ? sampleLines.get(i) : null;
    if (StringUtils.isEmpty(line)) {
        return result;
    }
    List<String> fields = readLine(line);
    for (String field : fields) {
        Scanner scanner = new Scanner(field);
        scanner.useDelimiter(Character.toString(separator.getSeparator()));
        // called integer but we are looking for long in Java parlance
        if (scanner.hasNextLong()) {
            result.add(INTEGER);
            scanner.next();
        } else if (scanner.hasNextDouble()) {
            result.add(DECIMAL);
            scanner.next();
        } else if (scanner.hasNextBoolean()) {
            result.add(BOOLEAN);
            scanner.next();
        } else {
            String text = scanner.hasNext() ? scanner.next() : StringUtils.EMPTY;
            switch (text) {
            case "":
                result.add(EMPTY);
                break;
            default: // used to detect a stable length of a field (may be it is a date or a pattern)
                result.add(text.length());
            }
        }
        scanner.close();
    }
    return result;
}