Scanner: next() : Scanner « java.util « Java by API






Scanner: next()

 
/**
 *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: findWithinHorizon(String pattern, int horizon)
7.Scanner: hasNext()
8.Scanner: hasNextBoolean()
9.Scanner: hasNextDouble()
10.Scanner: hasNextInt()
11.Scanner: hasNextLine()
12.Scanner: nextBoolean()
13.Scanner: nextDouble()
14.Scanner: nextInt()
15.Scanner: nextLine()
16.Scanner: useDelimiter(String pattern)