Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AverageLine {
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in);
- System.out.println("Enter string: ");
- String line = keyboard.nextLine();
- System.out.println("Average is: " + getAverage(line));
- }
- public static int getCount(String line)
- {
- int count=0;
- Scanner chopper = new Scanner(line);
- while(chopper.hasNextInt())
- {
- chopper.nextInt();
- count++;
- }
- return count;
- }
- public static int getSum(String line)
- {
- int sum=0;
- Scanner chopper = new Scanner(line);
- while(chopper.hasNextInt())
- {
- sum=sum + chopper.nextInt();
- }
- return sum;
- }
- public static double getAverage(String line)
- {
- double average;
- average = (double)getSum(line)/getCount(line);
- return average;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement