Example usage for java.util Scanner hasNextBoolean

List of usage examples for java.util Scanner hasNextBoolean

Introduction

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

Prototype

public boolean hasNextBoolean() 

Source Link

Document

Returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false".

Usage

From source file:Main.java

public static void main(String[] args) {

    String s = "Hello true World! 1 + 1 = 2.0 ";

    Scanner scanner = new Scanner(s);

    while (scanner.hasNext()) {

        // if the next is boolean, print found and the boolean
        if (scanner.hasNextBoolean()) {
            System.out.println("Found :" + scanner.nextBoolean());
        }/*from   w w  w  . j a v  a 2 s .  c o  m*/

        // if a boolean is not found, print "Not Found" and the token
        System.out.println("Not Found :" + scanner.next());
    }
    scanner.close();
}

From source file:Main.java

public static void main(String[] args) {

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

    Scanner scanner = new Scanner(s);

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

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

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    // Write output to a file.
    FileWriter fout = new FileWriter("test.txt");
    fout.write("int: 1  double 1.0  boolean true");
    fout.close();/*  w ww. j av  a  2 s  . c o  m*/

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

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            System.out.println("int: " + src.nextInt());
        } else if (src.hasNextDouble()) {
            System.out.println("double: " + src.nextDouble());
        } else if (src.hasNextBoolean()) {
            System.out.println("boolean: " + src.nextBoolean());
        } else {
            System.out.println(src.next());
        }
    }
    fin.close();
}

From source file:ScanMixed.java

public static void main(String args[]) throws IOException {
    int i;//from   w ww.ja  v  a  2  s  .  c  o m
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("Testing Scanner 10 12.2 one true two false");
    fout.close();

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

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            i = src.nextInt();
            System.out.println("int: " + i);
        } else if (src.hasNextDouble()) {
            d = src.nextDouble();
            System.out.println("double: " + d);
        } else if (src.hasNextBoolean()) {
            b = src.nextBoolean();
            System.out.println("boolean: " + b);
        } else {
            str = src.next();
            System.out.println("String: " + str);
        }
    }

    fin.close();
}

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    int i;/*from  w  ww . j a  va2s  . c  om*/
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("string true false 1 2 3 4.12");
    fout.close();

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

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            i = src.nextInt();
            System.out.println("int: " + i);
        } else if (src.hasNextDouble()) {
            d = src.nextDouble();
            System.out.println("double: " + d);
        } else if (src.hasNextBoolean()) {
            b = src.nextBoolean();
            System.out.println("boolean: " + b);
        } else {
            str = src.next();
            System.out.println("String: " + str);
        }
    }

    fin.close();
}

From source file:MainClass.java

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

    int i;/*from   w  w  w. j a v a2 s.co m*/
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("Testing Scanner 10 12.2 one true two false");
    fout.close();

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

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            i = src.nextInt();
            System.out.println("int: " + i);
        } else if (src.hasNextDouble()) {
            d = src.nextDouble();
            System.out.println("double: " + d);
        } else if (src.hasNextBoolean()) {
            b = src.nextBoolean();
            System.out.println("boolean: " + b);
        } else {
            str = src.next();
            System.out.println("String: " + str);
        }
    }

    fin.close();
}

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

/**
 * Performs type analysis for fields of the record at the specified index.
 * //from  w  ww.  ja  va2  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;
}