Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public class AverageLine {
  2.  
  3. public static void main(String[] args) {
  4. Scanner keyboard = new Scanner(System.in);
  5. System.out.println("Enter string: ");
  6. String line = keyboard.nextLine();
  7.  
  8. System.out.println("Average is: " + getAverage(line));
  9. }
  10.  
  11. public static int getCount(String line)
  12. {
  13. int count=0;
  14. Scanner chopper = new Scanner(line);
  15. while(chopper.hasNextInt())
  16. {
  17. chopper.nextInt();
  18. count++;
  19. }
  20. return count;
  21. }
  22.  
  23. public static int getSum(String line)
  24. {
  25. int sum=0;
  26. Scanner chopper = new Scanner(line);
  27. while(chopper.hasNextInt())
  28. {
  29. sum=sum + chopper.nextInt();
  30. }
  31. return sum;
  32. }
  33.  
  34. public static double getAverage(String line)
  35. {
  36. double average;
  37. average = (double)getSum(line)/getCount(line);
  38. return average;
  39. }
  40.  
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement