Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. scan = new Scanner(System.in);
  2. try {
  3. System.out.print("Please enter the filename for the integer values: ");
  4. File file = new File(scan.nextLine());
  5. scan = new Scanner(file);
  6. System.out.println("Integer Array Contents: ");
  7. String del = ", ";
  8. int totalOdd = 0;
  9. int lastZero = -1; //-1 usually indicates that it doesn't exist.
  10. int minimum = Integer.MAX_VALUE;
  11. int maximum = Integer.MIN_VALUE;
  12. int sum = 0;
  13. double mean;
  14. int totalIndex = 0;
  15. int[] oddNumbers = new int[15];
  16. int arrayIndex = 0;
  17. while (scan.hasNextLine()) {
  18. int num = Integer.parseInt(scan.next()); //parse the number into an integer.
  19. System.out.print(del + num);
  20. del = ", ";
  21. if (num%2!= 0) { //it's odd
  22. oddNumbers[arrayIndex] = num; //you have to use the arrayIndex variable here
  23. arrayIndex++;
  24. totalOdd++; //technically we could combine these vars if we wanted
  25. }
  26. if (num == 0) {
  27. lastZero= totalIndex;
  28. }
  29.  
  30. if (num < minimum) {
  31. minimum = num;
  32. }
  33.  
  34. if (num > maximum) {
  35. maximum = num;
  36. }
  37. sum += num;
  38. totalIndex++;
  39. }
  40.  
  41. mean = sum/totalIndex;
  42. System.out.println();
  43. System.out.println("Total odd numbers: " + totalOdd);
  44. System.out.print("Odd numbers are: ");
  45. for (int i = 0; i < totalIndex; i++) {
  46. System.out.print(oddNumbers[i] + " ");
  47. }
  48.  
  49. System.out.println();
  50. System.out.println("Index of last zero: " + lastZero);
  51. System.out.println("Minimum: " + minimum);
  52. System.out.println("Maximum: " + maximum);
  53. System.out.println("Sum: " + sum);
  54. System.out.println("Element mean is: " + mean);
  55. scan.close();
  56. } catch (Exception ex) {
  57. ex.printStackTrace();
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement