Hello,

I am working on a program that generates 100 random integers and then sorts them into even and odd. When i go to compile the program, it tells me that it cannot find variables randonum, evennum, and oddnum. I'm new to arrays so i'm guessing i declared/ defined them wrong. Thanks in advance for the help, i'm looking forward to becoming a part of the community!

import static java.lang.Math.*;
public class ArrayEvenOdd
{
		public static void main (String []args)
		{
				int evencount = 0;
				int oddcount = 0;
				int evennum [ ] = new int [ 100 ] ;
				int oddnum [ ] = new int [ 100 ];
				int randonum [ ] = new int [ 100 ];
				for (int j  = 0; j < randonum.length; j++ ) {
					randonum [j] = (int) ( random() * 25);
				}
		
				for (int y = 0; y < randomum.length; y++ ){
					if (randonum [y] % 2 == 0) {
						evennum( y, evencount );
						evencount++;
						
					} else {
						oddnum( y, oddcount );
						oddcount++;
					
					System.out.println( "The even numbers were:" );
					display ( evennum );
					System.out.println();
					System.out.println( "The odd numbers were:" );
					display ( oddnum );
					}
				}
		}
		
		public static int [] evennum( int num, int count )
		{
			evennum [ count ] = randonum [ num ];
			return evennum;
		}
		
		public static int [] oddnum( int num, int count )
		{
			oddnum [ count ] = randonum [ num ];
			return oddnum;
		}
		
		public static void display( int array[] )
		{
			for (int j  = 0; j < array.length; j++ )
					if ( array [ j ] != 0) {
					System.out.println( array[j] );
					}
		}
		
				
			
}

You have an error in spelling first thing I notice
randonum then in your second loop you tried spelling it randomum;
make sure spelling is correct.

You have an error in spelling first thing I notice
randonum then in your second loop you tried spelling it randomum;
make sure spelling is correct.

that's a start, thanks!

still have 6 error messages though :-/. lets keep going!

When you get errors, it helps if you copy and paste here the FULL text of the error message.

Two things are wrong with your code:

The first is in the second for loop where you check the random numbers...you used the variable "randomum" when it should be "randonum".

The second problem is more complex, you see you declared the counters and the arrays as local variables in a static method. This creates a problem, you cannot reference local variables(from a method) in another method because the second method being called has no way to see what variables the original declared.

Thus what you should do is declare the counters and the arrays outside the main method and make them static.

then i would personally just do something like this

int a=0;
int b= 0;
for (int y = 0; y < randonum.length; y++ ){
if(randonum[y]%2==0){
evennum[a]=randonum[y];
a++;
}
else{ oddnum[b]=1 
b++;
}
}
System.out.println("Even numbers are: ");
For(int i=0;i<evennum.length;i++){
System out println(""+evennum[i]);
}
System.out.println("Odd numbers are: ");
For(int i=0;i<oddnum.length;i++;i++)
System.out.println(""+evennum[i]);

With our way
I see that you are passing num and count in your arguments but I see no other reference to them in your code.

Are you trying to help the OP find his problem
Or to find the problem for him?
What does the OP learn if you do the work of finding the problem?

"Give a man a fish vs teaching him to fish"

commented: Made good points to another poster about HELPING someone understand, rather than just giving them an answer. +3

With our way
I see that you are passing num and count in your arguments but I see no other reference to them in your code.

those get replaced by the variables when referenced no?
and thanks to the others for the help

The second problem is more complex, you see you declared the counters and the arrays as local variables in a static method. This creates a problem, you cannot reference local variables(from a method) in another method because the second method being called has no way to see what variables the original declared.

Thus what you should do is declare the counters and the arrays outside the main method and make them static. Quoted: Zepplin

If we are not declaring them within the main method, where should i be declaring them?
within evennum and oddnum?

Error messages:

ArrayEvenOdd.java:35: cannot find symbol
symbol : variable evennum
location: class ArrayEvenOdd
evennum [ count ] = randonum [ num ];
^
ArrayEvenOdd.java:35: cannot find symbol
symbol : variable randonum
location: class ArrayEvenOdd
evennum [ count ] = randonum [ num ];
^
ArrayEvenOdd.java:36: cannot find symbol
symbol : variable evennum
location: class ArrayEvenOdd
return evennum;
^
ArrayEvenOdd.java:41: cannot find symbol
symbol : variable oddnum
location: class ArrayEvenOdd
oddnum [ count ] = randonum [ num ];
^
ArrayEvenOdd.java:41: cannot find symbol
symbol : variable randonum
location: class ArrayEvenOdd
oddnum [ count ] = randonum [ num ];
^
ArrayEvenOdd.java:42: cannot find symbol
symbol : variable oddnum
location: class ArrayEvenOdd
return oddnum;
^
6 errors

You should declare them outside of any method at the very beginning of the class, that way any(keep in mind that in this case the variables have to be static because your methods are static) method can use them. By doing that they will behave like global variables.

You should declare them outside of any method at the very beginning of the class, that way any(keep in mind that in this case the variables have to be static because your methods are static) method can use them. By doing that they will behave like global variables.

Ok, so how do i modify my declarations to be static?

Thanks for the help btw!

Use the static modifier in the variable declaration.
Ex.

static int zero = 0;

P.S I couldn't help to notice that these are basic question about programming, so I recommend that you read this thread: http://www.daniweb.com/software-development/java/threads/99132 it has many resources from which you can learn.

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.