Scanner: hasNextDouble() : Scanner : java.util : Java by API examples (example source code) Organized by topic

Java by API
C++
PHP
Java by API Home »  java.util   » [  Scanner  ]   
 



Scanner: hasNextDouble()

/**
 *Output:

 */

import java.util.Scanner;

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

    int count = 0;
    double sum = 0.0;

    System.out.println("Enter numbers to average.");

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

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

           
       
Related examples in the same category
1.  new Scanner(FileReader file)
2.  new Scanner(InputStream source)
3.  new Scanner(String instr)
4.  new Scanner(Readable source)
5.  Scanner: findInLine(String str)
6.  Scanner: hasNext()
7.  Scanner: hasNextBoolean()
8.  Scanner: hasNextInt()
9.  Scanner: next()
10.  Scanner: nextBoolean()
11.  Scanner: nextDouble()
12.  Scanner: nextInt()
13.  Scanner: useDelimiter(String pattern)
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.