Hey guys I need a little bit of help. I am writing a program that generates 100 random numbers and then stores then in an arrays (2, even and odd numbers) I need a little help with passing the array into the function and generating random numbers. What am I doing wrong here. I am getting error saying that the datatype for arraySum is not applicable for sortNumbers. Also am I generating and adding the random numbers correctly? Thanks! Here's me code:

import java.util.*;

public class arraySum {
	public static void main( String args[] )
	{
		int randomNumbers[] = {(int)(100.0 * Math.random()) + 1};
		sortNumbers( randomNumbers );

				
				
	}
		
	public static void sortNumbers( int oddNumbers[], int evenNumbers[] )

		{
		
				
				for (int j = 0; j < randomNumbers.length; j++)
				{
					
					if ( randomNumbers[j] % 2 == 0)
					{
						evenNumbers+=randomNumbers[j];
					}
					else
				{
						oddNumbers+=randomNumbers[j];
				}
				
		
	}
}

getting error saying that the datatype for arraySum is not applicable for sortNumbers

Please copy and paste here the full text of the error message.

int randomNumbers[] = {(int)(100.0 * Math.random()) + 1};

What do you want to happen with this statement?

I get this:


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method sortNumbers(int[], int[]) in the type arraySum is not applicable for the arguments (int[])

at arraySum.main(arraySum.java:7)

public static void sortNumbers( int oddNumbers[], int evenNumbers[] )
sortNumbers( randomNumbers );

So here we have a method declaration that requires two int arrays as parameters
And we have a method call that passes only one int array as parameter.
That's not legal Java.

First you need to design what you want the code to do.
Then you can write the code to do it.
What do you want the method sortNumbers to do?
What do you pass to it as args?
What does it do to those args?
What does it return (if anything)?

I am still confused syntactically about how to write this. This is my progress so far.

import java.util.*;

public class arraySum {
	public static void main( String args[] )
	{
		int randomNumbers[] = {(int)(25.00 * Math.random()) + 1};
		sortNumbers( randomNumbers );

		// Declare and generate an array of 100 integers between 0 and 25
		// Send that array to the sortNumbers method.
				
	}
		
	public static int[] sortNumbers( int randomNumbers[] ) {
		
		int evenNumbers[] = {};
		int oddNumbers[] = {};
		
		// Declare 2 arrays, one for even digits, and one for odd digits.
		
				
			for (int j = 0; j < randomNumbers.length; j++)
			{
				
					if ( randomNumbers[j] % 2 == 0)
					{
						evenNumbers += randomNumbers[j];
					}
					else
				{
						oddNumbers += randomNumbers[j];
				}
				
		
}
}
}

I am still confused syntactically about how to write this

To assign a value to an element of an array:
ARRAYNAME[<element index>] = <the value to go in the element>;

To move to the next element in the array, increment <element index>

So in this case I would have to do:

evenNumbers[elementIndex] = randomNumbers[j]

So in the for loop I would have to add elementindex and set it to 0;
int elementindex = 0,0 ?

And then in the loop:
elementindex++;

Is this correct?

Almost. Use this to init the index: int elementindex = 0;
You'll need one for each array.

Okay so all together the for loop would look like this?

for (int j = 0; j < randomNumbers.length; j++, int elementindexE = 0; int elementindexO = 0);
			{
 
					if ( randomNumbers[j] % 2 == 0)
					{
						evenNumbers[elementIndexE] = randomNumbers[j];
                                                elementIndexE++;

					}
					else
				{
						oddNumbers[elementIndexO] = randomNumbers[j];
                                                elementIndexO++;

				}

This is going to be slow if you ask about each few lines of code.
Put the code in your program, compile it and if ok, try executing it.

Okay! Finally finished it. Thanks for the help everyone.

public class arraySum {
	public static void main( String args[] )
	{
		int randomNumbers[] = new int [100];
		
		for(int i = 0; i<100;i++) {
			randomNumbers[i] = (int) (Math.random()*25)	;
			
		}
		
		sortNumbers( randomNumbers );
		

	
				
	}
		
	public static void sortNumbers( int randomNumbers[] ) {
		
		int evenNumbers[] = new int[100]; 
		int oddNumbers[] = new int[100];
		int x = 0;
		
				 
			for(int j = 0; j < randomNumbers.length; j++, x++) {
				
					if ( randomNumbers[j] % 2 == 0) {
						
						evenNumbers[x] = randomNumbers[j];
						
						
					}
					
					else {
						
						oddNumbers[x] = randomNumbers[j];
						
					}
					
			}
		
			
	
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.