Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
<pre>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;
            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();
        }
    }
}



My output:

Integer Array Contents: 
-3, 2, 0, 0, 1, -5
Total odd numbers: 3
Odd numbers are: -3 1 -5 0 0 0 
Index of last zero: 3
Minimum: -5
Maximum: 2
Sum: -5
Element mean is: 0.0


Why are those 3 0's under "Odd numbers are: " and how do I get rid of them to just show the other numbers.
Why is the mean not working? The mean is supposed to be: -0.833

What I have tried:

I originally had many problems with formatting, min and max, but now these are what I'm down to and can't figure out what I'm doing wrong.
Posted
Updated 24-Apr-17 8:39am
Comments
ZurdoDev 24-Apr-17 13:47pm    
Just debug your code and step through it line by line and you can see exactly what is happening.
way245 24-Apr-17 13:59pm    
I did. Thats why I'm here.
ZurdoDev 24-Apr-17 14:01pm    
You asked why the 0s were showing up. Debug your code and watch what happens on each line. You'll find why it is doing what it is doing.
way245 24-Apr-17 14:05pm    
There's no errors with the code, so the debugger isn't helping me. I know it has to do with my for loop. Do you have any ideas?
ZurdoDev 24-Apr-17 14:09pm    
"There's no errors with the code, so the debugger isn't helping me." - This is a common misunderstanding of what a debugger is. A debugger allows you to step through line by line and see the values of variables and watch them change as each line executes. A debugger does not find errors. That is the compiler. It's the most important tool a developer has and is much easier to use than some people realize.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
It's pretty simple: totalIndex counts each line you read, totalOdd counts the number of odd numbers you find.
And then you print the odd numbers using the wrong number:
Java
System.out.print("Odd numbers are: ");
for (int i = 0; i < totalIndex; i++) {
    System.out.print(oddNumbers[i] + " ");
}
Change totalIndex to totalOdd and your spurious zeros will vanish.
And your mean? That's integer division for you!
Your sum is -3 + 2 + 0 + 0 + 1 - 5 == -5, and the number of elements is 6. In integer division, -5 / 6 is 0.
Change your sum to a double and it should work.
 
Share this answer
 
Comments
Rick York 26-Apr-17 11:56am    
The sum doesn't necessarily have to be changed to a double but the division definitely has to be done with two doubles.
OriginalGriff 26-Apr-17 12:01pm    
No, it doesn't - but it makes sense to, because that way it doesn't get treated as an integer later by accident. Making it a double means that the system will perform an implicit cast on the count when you do the division and generate a double result.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900