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

public static void main(String args[]) {
    String instr = "Name: Joe Age: 28 ID: 77";

    Scanner conin = new Scanner(instr);

    conin.findInLine("Age:"); // find Age

    if (conin.hasNext())
        System.out.println(conin.next());
    else/*ww w  .  j  a va2s .  co m*/
        System.out.println("Error!");

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(FileSystems.getDefault().getPath("logs", "access.log"));
    System.out.println(scanner.nextLine());

    scanner.close();/* ww w .j  a  va  2s.  c o m*/
}

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

    // find a pattern of any letter plus "com"
    System.out.println(scanner.findInLine(Pattern.compile(".com")));

    // print the next line of the string
    System.out.println(scanner.nextLine());

    scanner.close();/*from  ww  w .  j a va2  s.c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("data.txt");
    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();

        Scanner lineScanner = new Scanner(line);
        lineScanner.useDelimiter(",");
        while (lineScanner.hasNext()) {
            String part = lineScanner.next();
            System.out.print(part + ", ");
        }//from   w  w  w.ja  v  a 2s  . co  m
        System.out.println();
    }
}

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

    System.out.println(scanner.hasNext(Pattern.compile(".com")));

    System.out.println(scanner.hasNext(Pattern.compile("1")));

    // print the rest of the string
    System.out.println(scanner.nextLine());

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

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

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

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

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

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

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

    // change the locale of the scanner
    scanner.useLocale(Locale.ENGLISH);

    // display the new locale
    System.out.println(scanner.locale());

    scanner.close();//from  w  w w .  ja  va2 s  .co m
}

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

    // assign locale as US to recognize double numbers in a string
    scanner.useLocale(Locale.US);

    while (scanner.hasNext()) {
        // check if the scanner's next token is a double
        System.out.println(scanner.hasNextDouble());
        System.out.println(scanner.next());
    }//from w  ww.  ja va 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 ";
    Scanner scanner = new Scanner(new StringReader(s));

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

    // change the radix of the scanner
    scanner.useRadix(32);/*  www .j  a  va 2s .  co  m*/

    // display the new radix
    System.out.println(scanner.radix());

    scanner.close();
}

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

    // check if the scanner's next token matches "c"
    System.out.println(scanner.hasNext("c"));

    // check if the scanner's next token matches "="
    System.out.println(scanner.hasNext("="));

    // print the rest of the string
    System.out.println(scanner.nextLine());
    scanner.close();//w w  w . j a  v  a  2  s .c  o  m
}