Example usage for java.util Scanner nextInt

List of usage examples for java.util Scanner nextInt

Introduction

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

Prototype

public int nextInt() 

Source Link

Document

Scans the next token of the input as an int .

Usage

From source file:Main.java

public static void main(String[] ap) {
    int val;
    try {/*from  www. ja  v a  2  s . c om*/
        Scanner sc = new Scanner(System.in); // Requires J2SE 1.5
        val = sc.nextInt();
    } catch (NumberFormatException ex) {
        System.err.println("Not a valid number: " + ex);
        return;
    }
    System.out.println("I read this number: " + val);
}

From source file:Main.java

public static void main(String[] args) {
    String str = "start 1 2 123455";
    Scanner sc = new Scanner(str);
    String param1 = sc.next();/*from   www  .j  a va  2  s .com*/
    int param2 = sc.nextInt();
    int param3 = sc.nextInt();
    int param4 = sc.nextInt();
    System.out.println(param1 + "\t" + param2 + "\t" + param3 + "\t" + param4);
}

From source file:ReadStdinInt15.java

public static void main(String[] ap) {
    int val;
    try {/*from w ww  .  ja v a2s .c o m*/
        Scanner sc = Scanner.create(System.in); // Requires J2SE 1.5
        val = sc.nextInt();
    } catch (NumberFormatException ex) {
        System.err.println("Not a valid number: " + ex);
        return;
    }
    System.out.println("I read this number: " + val);
}

From source file:StackTraceTest.java

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter n: ");
    int n = in.nextInt();
    factorial(n);/*from ww  w.j av  a2 s.  co  m*/
}

From source file:BigIntegerTest.java

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("How many numbers do you need to draw? ");
    int k = in.nextInt();

    System.out.print("What is the highest number you can draw? ");
    int n = in.nextInt();

    /*//w  ww.ja v a 2 s . co m
     * compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k)
     */

    BigInteger lotteryOdds = BigInteger.valueOf(1);

    for (int i = 1; i <= k; i++)
        lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1)).divide(BigInteger.valueOf(i));

    System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");
}

From source file:ScanMixed.java

public static void main(String args[]) throws IOException {
    int i;//from ww w. j a va 2  s  .com
    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: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 (scanner.hasNextInt()) {
            System.out.println("Found :" + scanner.nextInt());
        }//from   w ww . j  a v  a2 s . c  o  m
        System.out.println("Not Found :" + scanner.next());
    }

    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();//from w  w w  .jav a2s. 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:MainClass.java

public static void main(String args[]) throws IOException {
    int i;/*from   w  w  w . ja v  a 2  s.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 a 2 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();
}