Example usage for java.util Scanner next

List of usage examples for java.util Scanner next

Introduction

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

Prototype

public String next() 

Source Link

Document

Finds and returns the next complete token from this scanner.

Usage

From source file:GenericReflectionTest.java

public static void main(String[] args) {
    // read class name from command line args or user input
    String name;/* ww w.  j  av a2 s .  co m*/
    if (args.length > 0)
        name = args[0];
    else {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter class name (e.g. java.util.Collections): ");
        name = in.next();
    }

    try {
        // print generic info for class and public methods
        Class<?> cl = Class.forName(name);
        printClass(cl);
        for (Method m : cl.getDeclaredMethods())
            printMethod(m);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String args[]) throws IOException {

    int count = 0;
    double sum = 0.0;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("2 3.4 5 6 7.4 9.1 10.5 done");
    fout.close();//from ww w  .  ja  v a  2 s  .  c om

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            sum += src.nextDouble();
            count++;
        } else {
            String str = src.next();
            if (str.equals("done"))
                break;
            else {
                System.out.println("File format error.");
                return;
            }
        }
    }

    fin.close();
    System.out.println("Average is " + sum / count);
}

From source file:ReflectionTest.java

public static void main(String[] args) {
    // read class name from command line args or user input
    String name;//from  w ww.j  a  v  a 2  s  .  c o  m
    if (args.length > 0)
        name = args[0];
    else {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter class name (e.g. java.util.Date): ");
        name = in.next();
    }

    try {
        // print class name and superclass name (if != Object)
        Class cl = Class.forName(name);
        Class supercl = cl.getSuperclass();
        String modifiers = Modifier.toString(cl.getModifiers());
        if (modifiers.length() > 0)
            System.out.print(modifiers + " ");
        System.out.print("class " + name);
        if (supercl != null && supercl != Object.class)
            System.out.print(" extends " + supercl.getName());

        System.out.print("\n{\n");
        printConstructors(cl);
        System.out.println();
        printMethods(cl);
        System.out.println();
        printFields(cl);
        System.out.println("}");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    System.exit(0);
}

From source file:Main.java

public static void main(String[] args) {
    String s = "java2s.com 1 + 1 = 2.0 ";

    Scanner scanner = new Scanner(s);

    scanner.useLocale(Locale.US);

    while (scanner.hasNext()) {
        // check if the scanner's next token is a short with a radix 4
        System.out.println(scanner.hasNextShort(4));

        System.out.println(scanner.next());
    }/*  ww  w.  j  a  v a  2  s. co  m*/

    scanner.close();
}

From source file:Main.java

public static void main(String args[]) throws IOException {

    int count = 0;
    double sum = 0.0;

    FileWriter fout = new FileWriter("test.txt");

    fout.write("2, 3.4,    5,6, 7.4, 9.1, 10.5, done");
    fout.close();//from  ww  w  .ja va2 s. c  om

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    src.useDelimiter(", *");

    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            sum += src.nextDouble();
            count++;
        } else {
            String str = src.next();
            if (str.equals("done"))
                break;
            else {
                System.out.println("File format error.");
                return;
            }
        }
    }

    fin.close();
    System.out.println("Average is " + sum / count);
}

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    int count = 0;
    double sum = 0.0;

    FileWriter fout = new FileWriter("test.txt");

    fout.write("2, 3.4,    5,6, 7.4, 9.1, 10.5, done");
    fout.close();/*  w w w  .  j a  v a  2s.com*/

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    src.useDelimiter(", *");

    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            sum += src.nextDouble();
            count++;
        } else {
            String str = src.next();
            if (str.equals("done"))
                break;
            else {
                System.out.println("File format error.");
                return;
            }
        }
    }

    fin.close();
    System.out.println("Average is " + sum / count);
}

From source file:com.precioustech.fxtrading.oanda.restapi.marketdata.historic.HistoricMarketDataOnDemandDemo.java

public static void main(String[] args) throws Exception {
    usage(args);/*  ww w.  ja  va2  s .  c om*/
    final String url = args[0];
    final String accessToken = args[1];
    HistoricMarketDataProvider<String> historicMarketDataProvider = new OandaHistoricMarketDataProvider(url,
            accessToken);
    Scanner scanner = new Scanner(System.in);
    scanner.useDelimiter(System.getProperty("line.separator"));
    System.out.print("Instrument" + TradingConstants.COLON);
    String ccyPair = scanner.next();
    TradeableInstrument<String> instrument = new TradeableInstrument<>(ccyPair.toUpperCase());

    System.out.print(
            "Granularity" + ArrayUtils.toString(CandleStickGranularity.values()) + TradingConstants.COLON);
    CandleStickGranularity granularity = CandleStickGranularity.valueOf(scanner.next().toUpperCase());
    System.out.print("Time Range Candles(t) or Last N Candles(n)?:");
    String choice = scanner.next();

    List<CandleStick<String>> candles = null;
    if ("t".equalsIgnoreCase(choice)) {
        System.out.print("Start Time(" + datefmtLabel + ")" + TradingConstants.COLON);
        String startStr = scanner.next();
        Date startDt = sdf.parse(startStr);
        System.out.print("  End Time(" + datefmtLabel + ")" + TradingConstants.COLON);
        String endStr = scanner.next();
        Date endDt = sdf.parse(endStr);
        candles = historicMarketDataProvider.getCandleSticks(instrument, granularity,
                new DateTime(startDt.getTime()), new DateTime(endDt.getTime()));
    } else {
        System.out.print("Last how many candles?" + TradingConstants.COLON);
        int n = scanner.nextInt();
        candles = historicMarketDataProvider.getCandleSticks(instrument, granularity, n);
    }
    System.out.println(center("Time", timeColLen) + center("Open", priceColLen) + center("Close", priceColLen)
            + center("High", priceColLen) + center("Low", priceColLen));
    System.out.println(repeat("=", timeColLen + priceColLen * 4));
    for (CandleStick<String> candle : candles) {
        System.out.println(center(sdf.format(candle.getEventDate().toDate()), timeColLen)
                + formatPrice(candle.getOpenPrice()) + formatPrice(candle.getClosePrice())
                + formatPrice(candle.getHighPrice()) + formatPrice(candle.getLowPrice()));
    }
    scanner.close();
}

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);

    while (scanner.hasNext()) {

        // if the next is byte, print found and the byte
        if (scanner.hasNextByte()) {
            System.out.println("Found :" + scanner.nextByte());
        }//from w w w.j a va  2 s  . c  o m
        System.out.println("Not Found :" + scanner.next());
    }

    scanner.close();
}

From source file:org.forgerock.openicf.connectors.googleapps.Main.java

public static void main(String[] args) throws Exception {
    System.out.println(/*from   w ww  .  j  a  v  a2  s  . c om*/
            "-------------------------------------------------------------------------------------------");
    System.out.println("Generate credentials for GoogleApps Connector");
    System.out.println(
            "-------------------------------------------------------------------------------------------");
    System.out.println("You have to created and registered App in Google API and Google API enabled.");
    System.out.println(
            "Add these credentials into configuration fields in Google Apps Connector. See readme.txt.");
    System.out.println(
            "-------------------------------------------------------------------------------------------");
    System.out.println("");
    String clientId;
    String clientSecret;
    File file = new File(CLIENTSECRETS_LOCATION);
    FileWriter fileWriter = new FileWriter(file);
    Scanner user_input = new Scanner(System.in);
    System.out.print("Enter Client ID: ");
    clientId = user_input.next();
    System.out.print("Enter Client Secret: ");
    clientSecret = user_input.next();

    JSONObject jsonObj = new JSONObject();
    JSONObject jsonClient = new JSONObject();
    jsonClient.put("client_id", clientId);
    jsonClient.put("client_secret", clientSecret);
    jsonObj.put("installed", jsonClient);

    try {
        fileWriter.write(jsonObj.toJSONString());

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        fileWriter.flush();
        fileWriter.close();
    }

    if (file.exists() && file.isFile()) {
        System.out.println("");
        Map<String, Object> configMap = getConfigurationMap(file);
        System.out.println("");
        System.out.println("Generated credentials:");
        System.out.println(JSON_FACTORY.toPrettyString(configMap));
        System.out.println("");
        System.out.println("--------------------------------------------------------------------");
        System.out.println("Finished.");
        System.out.println("--------------------------------------------------------------------");
        return;
    } else {
        System.err.println("Invalid client secret path. File not exits " + file.getPath());
    }

    String fileName = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI())
            .getName();
    System.out.print("Usage: java -jar " + fileName + " <path to client_secrets.json>");
}

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);

    while (scanner.hasNext()) {

        // if the next is a int, print found and the int with radix 4
        if (scanner.hasNextInt()) {
            System.out.println("Found :" + scanner.nextInt(4));
        }//from w  w w .j a  v a 2s  . c o  m
        System.out.println("Not Found :" + scanner.next());
    }

    scanner.close();
}