Example usage for java.util Scanner nextDouble

List of usage examples for java.util Scanner nextDouble

Introduction

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

Prototype

public double nextDouble() 

Source Link

Document

Scans the next token of the input as a double .

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.useLocale(Locale.US);

    while (scanner.hasNext()) {

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

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

    scanner.close();
}

From source file:ScanMixed.java

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

From source file:MainClass.java

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

    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  2  s . co m*/

    FileReader fin = new FileReader("Test.txt");
    Scanner src = new Scanner(fin);
    // Set delimiters to space and comma.
    // ", *" tells Scanner to match a comma and zero or more spaces as
    // delimiters.

    src.useDelimiter(", *");

    // Read and sum numbers.
    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            System.out.println(src.nextDouble());
        } else {
            break;
        }
    }
    fin.close();
}

From source file:zappos.ZapposProblemSolver.java

/**
 * @param args the command line arguments
 *///  w  w w. j  av a2  s. c  o m

public static void main(String[] args) {

    ZapposProblemSolver problemSolver = new ZapposProblemSolver();
    System.out.println("Enter number of products and total amount ");
    Scanner sin = new Scanner(System.in);
    int no_of_products = sin.nextInt();
    double total_amount = sin.nextDouble();

    if (no_of_products > 0 && total_amount > 0) {
        if (problemSolver.initialize()) {
            problemSolver.getPriceCombinations(no_of_products, total_amount);
            problemSolver.getZapposProductDetails();
            problemSolver.displayZapposProducts();
        }
    } else
        System.out.println("Sorry! No products are found for this input.");

}

From source file:bundestagswahl.benchmark.BWBenchmark.java

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

    resultTime = new double[6];

    Scanner scanner = new Scanner(System.in);
    System.out.print("Please enter URL: ");
    serverUrl = scanner.nextLine();/*from w w w.j  av a  2 s.  c om*/
    System.out.print("Please enter number of terminals: ");
    numberTerminals = scanner.nextInt();
    System.out.print("Please enter number of requests: ");
    numberRequests = scanner.nextInt();
    System.out.print("Please enter delay between two requests in seconds: ");
    requestDelay = scanner.nextDouble();
    scanner.close();

    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    cm.setMaxTotal(numberTerminals);
    HttpClient httpclient = new DefaultHttpClient(cm);
    httpclient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 300000)
            .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 300000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true);
    try {
        final CountDownLatch latch = new CountDownLatch(numberTerminals);
        for (int i = 0; i < numberTerminals; i++) {
            BenchmarkTerminal terminal = new BenchmarkTerminal(httpclient, latch, serverUrl, numberRequests,
                    requestDelay);
            terminal.start();
        }
        latch.await();
        httpclient.getConnectionManager().shutdown();
    } finally {

    }

    printResultTimes();
    System.out.println(" ");
    System.out.println("Done");
}

From source file:Matrix_Operations.java

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    //Allow user to enter number of columns
    int rows = getRowsNumberFromUser();
    int columns = getColumnNumberFromUser();
    double matrixA[][] = new double[rows][columns];

    //Enter values for matrix
    System.out.println("Enter values for each position in the matrix below. ");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            matrixA[i][j] = keyboard.nextDouble();

        }//from  w w  w  .j a  v a 2s. com
        System.out.println("");
    }

    showMatrix(matrixA);
    System.out.println("");
    RealMatrix A = MatrixUtils.createRealMatrix(matrixA);

    LUDecomposition lu = new LUDecomposition(A);

    showMatrix(matrixA);

    System.out.println(lu.getDeterminant());

}

From source file:aula1.Aula1.java

/**
 * @param args the command line arguments
 *///from   ww w .  ja v  a 2  s. c  o  m
public static void main(String[] args) {
    Scanner dados = new Scanner(System.in);
    int escolha;
    double ini;
    double fim;
    String exp;
    String info;
    int sens = 0;
    boolean valida = true;

    do {
        System.out.println(
                "Decida a operao: 0 para montar grfico, 1 para calcular limite, 2 para calcular qualquer expresso");
        escolha = dados.nextInt();
        switch (escolha) {
        case 0:
            System.out.println("Informe o inicio do dominio");
            ini = dados.nextDouble();
            System.out.println("Informe o fim do dominio");
            fim = dados.nextDouble();
            System.out.println("Informe a quantidade de casas decimais desejadas: ");
            sens = dados.nextInt();
            System.out.println("Informe a expresso: ");
            exp = dados.next();
            double[] x = montaDominio(ini, fim, sens);
            calcula(x, exp);
            valida = false;
            break;
        case 1:
            System.out.println("Informe a expresso: ");
            exp = dados.next();
            System.out.println("Informe o ponto limite: ");
            info = dados.next();
            try {
                calculaLimite(exp, info);
            } catch (Exception ex) {
                System.out
                        .println("Erro: " + ex.getMessage() + "Tentativade aplcao do teorema do confronto");

            }
            valida = false;
            break;
        case 2:
            System.out.println("Informe a expresso:");
            info = dados.next();
            try {
                System.out.println(conversor(info, "0"));
            } catch (Exception ex) {
                System.out.println("Erro: " + ex.getMessage());
            }
            valida = false;
            break;
        default:
            System.out.println("Escolha invlida!");
            break;
        }
    } while (valida);

    //        Double[] vet = new Double[3];         
    //        vet = lerDados();
    //        montaDominio(vet[0], vet[1], vet[2].intValue());
    //        calculaLimite(3);
}

From source file:formatMessage.VerifyInputScanner.java

public static double verifyDouble() {

    while (true) {
        Scanner input = new Scanner(System.in);
        try {/*from w w w  .j a va 2  s . c  om*/
            Double verified = input.nextDouble();

            return verified;
        } catch (InputMismatchException e) {
            System.out.println("Geen geldig nummer. Probeer opnieuw.");

        }
    }
}