Example usage for java.util Scanner skip

List of usage examples for java.util Scanner skip

Introduction

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

Prototype

public Scanner skip(String pattern) 

Source Link

Document

Skips input that matches a pattern constructed from the specified string.

Usage

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

    scanner.skip("java2s");

    System.out.println(scanner.nextLine());

    scanner.close();//from   ww  w.jav  a 2s.c o  m
}

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

    // skip the word that matches the pattern .com
    scanner.skip(Pattern.compile(".com"));

    System.out.println(scanner.nextLine());

    scanner.close();// ww w.j  a  v a  2  s  .  c  om
}

From source file:edu.umich.robot.soar.SoarDataCollector.java

public void collect(SoarAgent agent) {
    if (!isEnabled())
        return;// w  ww.  java2 s .  com

    logger.debug("Collecting data.");
    try {
        StringBuilder headerBuilder = new StringBuilder();
        StringBuilder formatBuilder = new StringBuilder();

        addStat(headerBuilder, "agent", formatBuilder, "%s");
        addStat(headerBuilder, "wall clock", formatBuilder, "%f");
        addStat(headerBuilder, "dc num", formatBuilder, "%d");
        addStat(headerBuilder, "kernel sec", formatBuilder, "%f");
        addStat(headerBuilder, "avg msec/dc", formatBuilder, "%f");
        addStat(headerBuilder, "cpu sec", formatBuilder, "%f");
        addStat(headerBuilder, "pf total", formatBuilder, "%d");
        addStat(headerBuilder, "average msec/pf", formatBuilder, "%f");
        addStat(headerBuilder, "wm current", formatBuilder, "%d");
        addStat(headerBuilder, "wm mean", formatBuilder, "%f");
        addStat(headerBuilder, "wm max", formatBuilder, "%d");
        addStat(headerBuilder, "wm additions", formatBuilder, "%d");
        addStat(headerBuilder, "wm removals", formatBuilder, "%d");
        addStat(headerBuilder, "max dc time cycle", formatBuilder, "%d");
        addStat(headerBuilder, "max dc time value", formatBuilder, "%d");
        addStat(headerBuilder, "max dc changes cycle", formatBuilder, "%d");
        addStat(headerBuilder, "max dc changes value", formatBuilder, "%d");
        addStat(headerBuilder, "max dc pf cycle", formatBuilder, "%d");
        addStat(headerBuilder, "max dc pf value", formatBuilder, "%d");
        addStat(headerBuilder, "epmem time", formatBuilder, "%f");
        addStat(headerBuilder, "epmem max time cycle", formatBuilder, "%d");
        addStat(headerBuilder, "epmem max time value", formatBuilder, "%f");
        addStat(headerBuilder, "epmem bytes", formatBuilder, "%d");
        addStat(headerBuilder, "epmem stores", formatBuilder, "%d");
        addStat(headerBuilder, "epmem time per dc", formatBuilder, "%f");
        addStat(headerBuilder, "smem time", formatBuilder, "%f");
        addStat(headerBuilder, "smem max time cycle", formatBuilder, "%d");
        addStat(headerBuilder, "smem max time value", formatBuilder, "%f");
        addStat(headerBuilder, "smem bytes", formatBuilder, "%d");
        addStat(headerBuilder, "smem retrieves", formatBuilder, "%d");
        addStat(headerBuilder, "smem queries", formatBuilder, "%d");
        addStat(headerBuilder, "smem stores", formatBuilder, "%d");
        addStat(headerBuilder, "smem time per dc", formatBuilder, "%f");

        headerBuilder.append("settings");

        final String header = headerBuilder.toString();
        final String format = formatBuilder.toString();

        if (pout == null) {
            try {
                FileOutputStream fos = new FileOutputStream(file.get());
                OutputStreamWriter out = new OutputStreamWriter(fos);
                pout = new PrintWriter(out);
            } catch (IOException e) {
                e.printStackTrace();
                file.set(null);
                return;
            }

            pout.println(header);
        }

        ClientAnalyzedXML response = new ClientAnalyzedXML();
        agent.getSoarAgent().ExecuteCommandLineXML("stats", response);

        long dc = response.GetArgInt(sml.sml_Names.getKParamStatsCycleCountDecision(), 0L);
        long deltaDc = dc - lastDc;
        lastDc = dc;
        if (dc < 1)
            return;
        double ksec = response.GetArgFloat(sml.sml_Names.getKParamStatsKernelCPUTime(), 0);
        double tsec = response.GetArgFloat(sml.sml_Names.getKParamStatsTotalCPUTime(), 0);
        long pf = response.GetArgInt(sml.sml_Names.getKParamStatsProductionFiringCount(), 0L);
        long wmcount = response.GetArgInt(sml.sml_Names.getKParamStatsWmeCount(), 0L);
        double wmmean = response.GetArgFloat(sml.sml_Names.getKParamStatsWmeCountAverage(), 0);
        long wmmax = response.GetArgInt(sml.sml_Names.getKParamStatsWmeCountMax(), 0L);

        long wmadd = response.GetArgInt(sml.sml_Names.getKParamStatsWmeCountAddition(), 0L);
        long wmrem = response.GetArgInt(sml.sml_Names.getKParamStatsWmeCountRemoval(), 0L);

        long maxdctimec = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleTimeCycle(), 0L);
        long maxdctimev = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleTimeValueUSec(), 0L);
        long maxdcwmcc = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleWMChangesCycle(), 0L);
        long maxdcwmcv = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleWMChangesValue(), 0L);
        long maxdcpfcc = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleFireCountCycle(), 0L);
        long maxdcpfcv = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleFireCountValue(), 0L);

        long epmemMaxTimeCycle = response
                .GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleEpMemTimeCycle(), 0);
        double epmemMaxTimeValue = response
                .GetArgFloat(sml.sml_Names.getKParamStatsMaxDecisionCycleEpMemTimeValueSec(), 0);
        long smemMaxTimeCycle = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleSMemTimeCycle(),
                0);
        double smemMaxTimeValue = response
                .GetArgFloat(sml.sml_Names.getKParamStatsMaxDecisionCycleSMemTimeValueSec(), 0);

        Scanner epmemTimeScanner = new Scanner(agent.getSoarAgent().ExecuteCommandLine("epmem -t"));
        double epmemTime = epmemTimeScanner.skip(".+: ").nextDouble();

        Scanner epmemStatsScanner = new Scanner(agent.getSoarAgent().ExecuteCommandLine("epmem -S"));
        long epmemStores = epmemStatsScanner.skip(".+: ").nextLong(); // Time == Stores (more or less)
        epmemStatsScanner.nextLine();
        epmemStatsScanner.nextLine(); // SQLite version
        long epmemBytes = epmemStatsScanner.skip(".+: ").nextLong();

        Scanner smemTimeScanner = new Scanner(agent.getSoarAgent().ExecuteCommandLine("smem -t"));
        double smemTime = smemTimeScanner.skip(".+: ").nextDouble();

        Scanner smemStatsScanner = new Scanner(agent.getSoarAgent().ExecuteCommandLine("smem -S"));
        smemStatsScanner.nextLine(); // SQLite version
        long smemBytes = smemStatsScanner.skip(".+: ").nextLong();
        smemStatsScanner.nextLine();
        smemStatsScanner.nextLine(); // Memory Highwater
        long smemRetrieves = smemStatsScanner.skip(".+: ").nextLong();
        smemStatsScanner.nextLine();
        long smemQueries = smemStatsScanner.skip(".+: ").nextLong();
        smemStatsScanner.nextLine();
        long smemStores = smemStatsScanner.skip(".+: ").nextLong();

        double wallClock = clock.getMillis() / 1000.0;

        double deltaSmemTime = smemTime - lastSmemTime;
        lastSmemTime = smemTime;
        double smemTotalTimePerDc = deltaDc > 0 ? deltaSmemTime / deltaDc : 0;

        double deltaEpmemTime = epmemTime - lastEpmemTime;
        lastEpmemTime = epmemTime;
        double epmemTotalTimePerDc = deltaDc > 0 ? deltaEpmemTime / deltaDc : 0;

        String out = String.format(format, agent.getSoarAgent().GetAgentName(), wallClock, dc, ksec,
                ((ksec * 1000.0) / dc), tsec, pf, ((ksec * 1000.0) / pf), wmcount, wmmean, wmmax, wmadd, wmrem,
                maxdctimec, maxdctimev, maxdcwmcc, maxdcwmcv, maxdcpfcc, maxdcpfcv, epmemTime,
                epmemMaxTimeCycle, epmemMaxTimeValue, epmemBytes, epmemStores, epmemTotalTimePerDc, smemTime,
                smemMaxTimeCycle, smemMaxTimeValue, smemBytes, smemRetrieves, smemQueries, smemStores,
                smemTotalTimePerDc);
        pout.print(out);
        //System.out.println(out);
        if (!printedSettings) {
            pout.print(getSettingsString(agent));
            printedSettings = true;
        }
        pout.println();
        if (flush.getAndSet(false)) {
            if (logger.isDebugEnabled())
                logger.debug("Flushing data to " + file.get());
            pout.flush();
        }

        agent.getSoarAgent().ExecuteCommandLine("stats -R"); // reset max stats
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

From source file:edu.umich.soar.DataCollector.java

/**
 * <p>//from  www.  j a v  a 2s .c om
 * Collect stats from the agent and write them to the output stream without
 * flushing.
 * 
 * <p>
 * Call this method for each agent in a kernel after a call to onUpdateEvent
 * (called each decision cycle) returns true.
 * 
 * <p>
 * A header line and the agent's current settings are written once the first
 * time this is called.
 * 
 * <p>
 * The data format is a simple comma separated values text document. Opens
 * directly with Microsoft Excel and LibreOffice.
 * 
 * <p>
 * Occasionally call flush() to guard against catastrophic failures.
 * 
 * <p>
 * This method must be called from the same thread that fired the event.
 * 
 * @param agent
 *            The agent to collect data from.
 */
public void collect(Agent agent) {
    if (!isEnabled())
        return;

    logger.debug("Collecting data.");
    try {
        PrintWriter pout = this.pout;
        boolean firstPass = pout == null;
        if (firstPass) {
            pout = new PrintWriter(out);
            this.pout = pout;
            pout.println(HEADER);
        }

        ClientAnalyzedXML response = new ClientAnalyzedXML();
        agent.ExecuteCommandLineXML("stats", response);

        long dc = response.GetArgInt(sml.sml_Names.getKParamStatsCycleCountDecision(), 0L);
        long deltaDc = dc - lastDc;
        lastDc = dc;
        if (dc < 1)
            return;

        // getKParamStatsKernelCPUTime returns seconds, divide to get msec
        double kmsec = response.GetArgFloat(sml.sml_Names.getKParamStatsKernelCPUTime(), 0) / 1000;
        double deltaKmsecTime = kmsec - lastKmsecTime;
        lastSmemTimeMsec = kmsec;
        double kmsecTotalTimePerDc = deltaDc > 0 ? deltaKmsecTime / deltaDc : 0;

        // getKParamStatsTotalCPUTime returns seconds, divide to get msec
        double tmsec = response.GetArgFloat(sml.sml_Names.getKParamStatsTotalCPUTime(), 0) / 1000;
        double deltaCpumsecTime = tmsec - lastCpumsecTime;
        lastCpumsecTime = tmsec;

        long pf = response.GetArgInt(sml.sml_Names.getKParamStatsProductionFiringCount(), 0L);
        long deltaPfCount = pf - lastPfCount;
        lastPfCount = pf;
        double meanMsecPerPf = deltaPfCount > 0 ? deltaKmsecTime / deltaPfCount : 0;

        long wmcount = response.GetArgInt(sml.sml_Names.getKParamStatsWmeCount(), 0L);

        // We want wmmean reset each call to collect so we can't use the stat
        //double wmmean = response.GetArgFloat(sml.sml_Names.getKParamStatsWmeCountAverage(), 0);
        long deltaWmCount = wmcount - lastWmCount;
        lastWmCount = wmcount;
        double meanWmCountPerDc = deltaDc > 0 ? deltaWmCount / deltaDc : 0;

        long wmmax = response.GetArgInt(sml.sml_Names.getKParamStatsWmeCountMax(), 0L);

        long wmaddTotal = response.GetArgInt(sml.sml_Names.getKParamStatsWmeCountAddition(), 0L);
        long deltaWmAdd = wmaddTotal - lastWmAdd;
        lastWmAdd = wmaddTotal;

        long wmremTotal = response.GetArgInt(sml.sml_Names.getKParamStatsWmeCountRemoval(), 0L);
        long deltaWmRem = wmremTotal - lastWmRem;
        lastWmRem = wmremTotal;

        long maxdctimec = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleTimeCycle(), 0L);
        long maxdctimev = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleTimeValueUSec(), 0L);
        long maxdcwmcc = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleWMChangesCycle(), 0L);
        long maxdcwmcv = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleWMChangesValue(), 0L);
        long maxdcpfcc = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleFireCountCycle(), 0L);
        long maxdcpfcv = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleFireCountValue(), 0L);

        long epmemMaxTimeCycle = response
                .GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleEpMemTimeCycle(), 0);
        double epmemMaxTimeValueMsec = response
                .GetArgFloat(sml.sml_Names.getKParamStatsMaxDecisionCycleEpMemTimeValueSec(), 0) * 1000;
        long smemMaxTimeCycle = response.GetArgInt(sml.sml_Names.getKParamStatsMaxDecisionCycleSMemTimeCycle(),
                0);
        double smemMaxTimeValueMsec = response
                .GetArgFloat(sml.sml_Names.getKParamStatsMaxDecisionCycleSMemTimeValueSec(), 0) * 1000;

        Scanner epmemTimeScanner = new Scanner(agent.ExecuteCommandLine("epmem -t"));
        // epmem and smem timers report seconds
        double epmemTimeMsec = epmemTimeScanner.skip(".+: ").nextDouble() * 1000;

        Scanner epmemStatsScanner = new Scanner(agent.ExecuteCommandLine("epmem -S"));
        long epmemStores = epmemStatsScanner.skip(".+: ").nextLong(); // Time == Stores (more or less)
        epmemStatsScanner.nextLine(); // Time (Stores)
        epmemStatsScanner.nextLine(); // SQLite version
        long epmemAndSmemBytes = epmemStatsScanner.skip(".+: ").nextLong();
        epmemStatsScanner.nextLine(); // Bytes
        epmemStatsScanner.nextLine(); // Memory Highwater
        long epmemQueries = epmemStatsScanner.skip(".+: ").nextLong();

        Scanner smemTimeScanner = new Scanner(agent.ExecuteCommandLine("smem -t"));
        // epmem and smem timers report seconds
        double smemTimeMsec = smemTimeScanner.skip(".+: ").nextDouble() * 1000;

        Scanner smemStatsScanner = new Scanner(agent.ExecuteCommandLine("smem -S"));
        smemStatsScanner.nextLine(); // SQLite version
        smemStatsScanner.nextLine(); // Bytes
        smemStatsScanner.nextLine(); // Memory Highwater
        long smemRetrieves = smemStatsScanner.skip(".+: ").nextLong();
        smemStatsScanner.nextLine(); // Retrieves
        long smemQueries = smemStatsScanner.skip(".+: ").nextLong();
        smemStatsScanner.nextLine(); // Queries
        long smemStores = smemStatsScanner.skip(".+: ").nextLong();

        double wallClock = (System.currentTimeMillis() - offset) / 1000.0;

        double deltaSmemTimeMsec = smemTimeMsec - lastSmemTimeMsec;
        lastSmemTimeMsec = smemTimeMsec;
        double smemTimeMsecPerDc = deltaDc > 0 ? deltaSmemTimeMsec / deltaDc : 0;

        double deltaEpmemTimeMsec = epmemTimeMsec - lastEpmemTimeMsec;
        lastEpmemTimeMsec = epmemTimeMsec;
        double epmemTimeMsecPerDc = deltaDc > 0 ? deltaEpmemTimeMsec / deltaDc : 0;

        String out = String.format(FORMAT, agent.GetAgentName(), wallClock, dc, deltaKmsecTime,
                kmsecTotalTimePerDc, deltaCpumsecTime, deltaPfCount, meanMsecPerPf, wmcount, meanWmCountPerDc,
                wmmax, deltaWmAdd, deltaWmRem, maxdctimec, maxdctimev, maxdcwmcc, maxdcwmcv, maxdcpfcc,
                maxdcpfcv, epmemTimeMsec, epmemMaxTimeCycle, epmemMaxTimeValueMsec, epmemAndSmemBytes,
                epmemQueries, epmemStores, epmemTimeMsecPerDc, smemTimeMsec, smemMaxTimeCycle,
                smemMaxTimeValueMsec, smemRetrieves, smemQueries, smemStores, smemTimeMsecPerDc);
        pout.print(out);
        //System.out.println(out);

        if (firstPass) {
            pout.print(getSettingsString(agent));
        }
        pout.println();

        // flush only if stopped
        if (stopTime != 0) {
            pout.flush();
        }

        agent.ExecuteCommandLine("stats -R"); // reset max stats
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

From source file:org.orekit.files.sp3.SP3Parser.java

/** Parses a header line from the SP3 file (line number 1 - 22).
 * @param lineNumber the current line number
 * @param line the line as read from the SP3 file
 * @param pi the current {@link ParseInfo} object
 * @throws OrekitException if a non-supported construct is found
 *//*  w  w w.  java 2  s  . c  om*/
private void parseHeaderLine(final int lineNumber, final String line, final ParseInfo pi)
        throws OrekitException {

    final SP3File file = pi.file;

    Scanner scanner = null;
    try {
        scanner = new Scanner(line).useDelimiter("\\s+").useLocale(Locale.US);

        // CHECKSTYLE: stop FallThrough check

        switch (lineNumber) {

        // version, epoch, data used and agency information
        case 1: {
            scanner.skip("#");
            final String v = scanner.next();

            final char version = v.substring(0, 1).toLowerCase().charAt(0);
            if (version != 'a' && version != 'b' && version != 'c') {
                throw new OrekitException(OrekitMessages.SP3_UNSUPPORTED_VERSION, version);
            }

            pi.hasVelocityEntries = "V".equals(v.substring(1, 2));

            final int year = Integer.parseInt(v.substring(2));
            final int month = scanner.nextInt();
            final int day = scanner.nextInt();
            final int hour = scanner.nextInt();
            final int minute = scanner.nextInt();
            final double second = scanner.nextDouble();

            final AbsoluteDate epoch = new AbsoluteDate(year, month, day, hour, minute, second,
                    TimeScalesFactory.getGPS());

            file.setEpoch(epoch);

            final int numEpochs = scanner.nextInt();
            file.setNumberOfEpochs(numEpochs);

            // data used indicator
            file.setDataUsed(scanner.next());

            file.setCoordinateSystem(scanner.next());
            file.setOrbitType(SP3OrbitType.valueOf(scanner.next()));
            file.setAgency(scanner.next());
            break;
        }

        // additional date/time references in gps/julian day notation
        case 2: {
            scanner.skip("##");

            // gps week
            file.setGpsWeek(scanner.nextInt());
            // seconds of week
            file.setSecondsOfWeek(scanner.nextDouble());
            // epoch interval
            file.setEpochInterval(scanner.nextDouble());
            // julian day
            file.setJulianDay(scanner.nextInt());
            // day fraction
            file.setDayFraction(scanner.nextDouble());
            break;
        }

        // line 3 contains the number of satellites
        case 3:
            pi.maxSatellites = Integer.parseInt(line.substring(4, 6).trim());
            // fall-through intended - the line contains already the first entries

            // the following 4 lines contain additional satellite ids
        case 4:
        case 5:
        case 6:
        case 7: {
            final int lineLength = line.length();
            int count = file.getSatelliteCount();
            int startIdx = 9;
            while (count++ < pi.maxSatellites && (startIdx + 3) <= lineLength) {
                final String satId = line.substring(startIdx, startIdx + 3).trim();
                file.addSatellite(satId);
                startIdx += 3;
            }
            break;
        }

        // the following 5 lines contain general accuracy information for each satellite
        case 8:
        case 9:
        case 10:
        case 11:
        case 12: {
            final int lineLength = line.length();
            int satIdx = (lineNumber - 8) * 17;
            int startIdx = 9;
            while (satIdx < pi.maxSatellites && (startIdx + 3) <= lineLength) {
                final SatelliteInformation satInfo = file.getSatellite(satIdx++);
                final int exponent = Integer.parseInt(line.substring(startIdx, startIdx + 3).trim());
                // the accuracy is calculated as 2**exp (in m) -> can be safely
                // converted to an integer as there will be no fraction
                satInfo.setAccuracy((int) Math.pow(2d, exponent));
                startIdx += 3;
            }
            break;
        }

        case 13: {
            file.setType(getFileType(line.substring(3, 5).trim()));

            // now identify the time system in use
            final String tsStr = line.substring(9, 12).trim();
            final TimeSystem ts = TimeSystem.GPS;
            if (!tsStr.equalsIgnoreCase("ccc")) {
                TimeSystem.valueOf(tsStr);
            }
            file.setTimeSystem(ts);

            switch (ts) {
            case GPS:
                pi.timeScale = TimeScalesFactory.getGPS();
                break;

            case GAL:
                pi.timeScale = TimeScalesFactory.getGST();
                break;

            case GLO:
            case QZS:
                throw new OrekitException(OrekitMessages.SP3_UNSUPPORTED_TIMESYSTEM, ts.name());

            case TAI:
                pi.timeScale = TimeScalesFactory.getTAI();
                break;

            case UTC:
                pi.timeScale = TimeScalesFactory.getUTC();
                break;

            default:
                pi.timeScale = TimeScalesFactory.getGPS();
                break;
            }
            break;
        }

        case 14:
            // ignore additional custom fields
            break;

        // load base numbers for the standard deviations of
        // position/velocity/clock components
        case 15: {
            // String base = line.substring(3, 13).trim();
            // if (!base.equals("0.0000000")) {
            //    // (mm or 10**-4 mm/sec)
            //    pi.posVelBase = Double.valueOf(base);
            // }

            // base = line.substring(14, 26).trim();
            // if (!base.equals("0.000000000")) {
            //    // (psec or 10**-4 psec/sec)
            //    pi.clockBase = Double.valueOf(base);
            // }
            break;
        }

        case 16:
        case 17:
        case 18:
            // ignore additional custom parameters
            break;

        case 19:
        case 20:
        case 21:
        case 22:
            // ignore comment lines
            break;
        default:
            // ignore -> method should only be called up to line 22
            break;
        }

        // CHECKSTYLE: resume FallThrough check

    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }

}