Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- scan = new Scanner(System.in);
- try {
- System.out.print("Please enter the filename for the integer values: ");
- File file = new File(scan.nextLine());
- scan = new Scanner(file);
- System.out.println("Integer Array Contents: ");
- String del = ", ";
- int totalOdd = 0;
- int lastZero = -1; //-1 usually indicates that it doesn't exist.
- int minimum = Integer.MAX_VALUE;
- int maximum = Integer.MIN_VALUE;
- int sum = 0;
- double mean;
- int totalIndex = 0;
- int[] oddNumbers = new int[15];
- int arrayIndex = 0;
- while (scan.hasNextLine()) {
- int num = Integer.parseInt(scan.next()); //parse the number into an integer.
- System.out.print(del + num);
- del = ", ";
- if (num%2!= 0) { //it's odd
- oddNumbers[arrayIndex] = num; //you have to use the arrayIndex variable here
- arrayIndex++;
- totalOdd++; //technically we could combine these vars if we wanted
- }
- if (num == 0) {
- lastZero= totalIndex;
- }
- if (num < minimum) {
- minimum = num;
- }
- if (num > maximum) {
- maximum = num;
- }
- sum += num;
- totalIndex++;
- }
- mean = sum/totalIndex;
- System.out.println();
- System.out.println("Total odd numbers: " + totalOdd);
- System.out.print("Odd numbers are: ");
- for (int i = 0; i < totalIndex; i++) {
- System.out.print(oddNumbers[i] + " ");
- }
- System.out.println();
- System.out.println("Index of last zero: " + lastZero);
- System.out.println("Minimum: " + minimum);
- System.out.println("Maximum: " + maximum);
- System.out.println("Sum: " + sum);
- System.out.println("Element mean is: " + mean);
- scan.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement