Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,
this is my code at the moment I figured to store the numbers in the integers the integer would have to be an integer array as all the odd numbers will be going into it but I am struggling to put the numbers into the lists so it would be very much appreciated if you could give me example code or point me in the right direction



Java
import java.util.Scanner;

public class OddEvenAverage {
    public static void main(String[] args) {
        Scanner Scan = new Scanner (OddEvenAverage.class.getResourceAsStream("Numbers.txt"));

        int numbersCounter = 0; //counts how many numbers in file
        int osum [] = new int [25]; //sum of all odd the numbers
        int esum [] = new int [25];  //sum of all even numbers
        int sum = 0;
        while (Scan.hasNext()){ //Starts the while loop
            // Read the next number.
            int numList = Scan.nextInt();
            if (numList %2==0 ) {
                // TODO 
            }
            // Display the last number read.
            //System.out.println(numList);
            sum = sum + numList;
            numbersCounter++;
        }
        System.out.println("Average: " + (sum/numbersCounter));
    }
}
Posted
Updated 23-May-15 2:25am
v2
Comments
Richard MacCutchan 23-May-15 8:27am    
Why do you need the two arrays, if you are just trying to calculate the sums? Or do you want the sums and the averages?
Adamdew93 23-May-15 8:28am    
I need to work out the averages of all of the even and odd numbers
Richard MacCutchan 23-May-15 12:52pm    
Then you just need to calculate the sum, and count the number, of each as you go through it. Something like:

int oddSum = 0;
int oddCount = 0;
int evenSum = 0;
int evenCount = 0;
// loop
Read next number
If ODD
Add number to oddSum
Add 1 to oddCount
Else
do the same for evens
At end of loop, calculate the averages and print them.
[no name] 23-May-15 12:07pm    
http://www.codeproject.com/Answers/993905/Does-anyone-know-what-bad-operand-types-for-binary#answer1
Adamdew93 27-May-15 6:47am    
Will I still need the if statement I already have or will I just need to change it

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