Scanner: hasNextDouble()
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);
}
}
Home
Java Book
Essential Classes
Java Book
Essential Classes
Scanner:
- Scanner
- Setting Delimiters
- findInLine( )
- findWithinHorizon( )
- skip( )
- new Scanner(FileReader file)
- new Scanner(InputStream source)
- Scanner: hasNext()
- Scanner: hasNextBoolean()
- Scanner: hasNextDouble()
- Scanner: hasNextInt()
- Scanner: hasNextLine()
- Scanner: next()
- Scanner: nextBoolean()
- Scanner: nextDouble()
- Scanner: nextInt()
- Scanner: nextLine()