Hey everyone, I am working with arrays, my program needs to take a list of 10 numbers and put them in each array depending on if they are even, odd, or negative. My program compiles but it does not run correctly it only displays 0 for each list. Thanks in advance

import TerminalIO.KeyboardReader;

 public class evenodd
{
  public static void main (String []args)
{
  KeyboardReader reader = new KeyboardReader();

  double[] even = new double[11];
  double[] odd = new double[11];
  double[] neg = new double[11];
  double num;
  int i;
  double oddl = 0;
  double evenl = 0;
  double negl = 0;

  System.out.print ("Enter Numbers: ");
  num = reader.readDouble();

  for(i = 1; i <= 9; i++)
{
  System.out.print ("Enter Numbers: ");
  num = reader.readDouble();
  
  if(num > 0)
{
  if(num / 2 != 0)
{
  oddl = num;
  odd[i] = oddl;
}
  else
{
  evenl = num;
  even[i] = evenl;
}
  negl = num;
  neg[i] = negl;
}
}
  System.out.println ("The Even Numbers Are: " + even[i]);
  System.out.println ("The Odd Numbers Are: " +odd[i]);
  System.out.println ("The Negative Numbers Are: " +neg[i]);

}
}

First thing you need to do is review arrays.
Printing even only prints one value: the value stored at index i of the array called "even". If you have values in an array, you have to lop over the array in some fashion to display them.
Your method of writing to the arrays is also peculiar, and I don't think it does what you think it does.

Then you might want to consider what it would mean for a double - like 3.546 - to be "even" or "odd".

Then you might want to look at the difference between the / and the % operators.

So after my if else statement I should write a loop that takes my numbers and place them in the loop? Because my biggest problem is getting the array to display correctly.

You have a lot of fixing there, but that's a good place to start. Yes, make a loop if you want to display the contents of an array. You can display the contents of several arrays in one loop. The body of the loop would look something like

System.out.printf("%d\t%d\t%d\n", array1[i],array2[i], array3[i]);

where the arrays are arrays of integers or longs and i is your loop counter. Look up the printf method for more about the formatted print string - that's documented, you can find it without my help.

Here is my view of your code... I added some comments so you can use it.
and some BIG suggestion... If you planning to continue to use JAVA learn how to indent your code... putting that much {{{}}} in same starting point (column1) is too much confusing and not readable. Look down in my code to see difference. It is simple for start... on every opened parenthesis add one TAB more.
Also arrays starts from 0, why do you start counters from 1? i thing that the problem why you create array with 11 fields.I leave it like that in code below but every other thing i started from 0, just not to be confused.

import java.io.*;

 public class evenodd
{
  public static void main (String [] args) throws Exception
	{
	  BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); //I preffered Bufferedreader, you can change it
	  double[] even=new double[11]; //Declarations
	  double[] odd=new double[11];
	  double[] negative=new double[11];
	  double number=0;
	  int ceven=0; //Counter for even values
	  int codd=0; //Counter for odd values
	  int cneg=0; //counter for negative values
	  
	  for (int i=1;i<9;i++)
	  {
		  System.out.println("Please input your number");
		  number=Double.parseDouble(reader.readLine()); //Converting string to double as you want doubles
		  if (number < 0) //If number is negative...
		  {
			  negative[cneg]=number; //... put it in negative array and..
			  cneg++; //increase negative counter by one
		  }
		  else if (number % 2 ==0) // if number is even  notice that checking is with % not number/2!=0
		  {
			  even[ceven]=number; //put it in array
			  ceven++; //increase even counter
		  }
		  else if (number % 2 !=0) //same as above...
		  {
			  odd[codd]=number;
			  codd++;
		  }
	  }
	  //now we have 3 arrays with numbers...
	  //Here goes routine for printing.. I'll print every array in one row, you can change this as you like
	  		System.out.print("Even Numbers are: ");  //I'm using print for printing in same line
	  	for (int i=0;i<ceven;i++) //going trought even counter to print while even array
		{
			System.out.print (" " + even[i]); //printing even numbers from array
		}
  		System.out.println();
	  	System.out.print("Odd Numbers are: "); 
	  	for (int i=0;i<codd;i++)
		{
			System.out.print (" " + odd[i]);
		}
  		System.out.println();
  		System.out.print("Negative Numbers are: "); 
	  	for (int i=0;i<cneg;i++)
		{
			System.out.print (" " + negative[i]);
		}
	}
  
}
commented: Please don't do his homework for him +0

Unless you're planning on following the guy around for the rest of his life solving his problems, you probably shouldn't get in the habit of doing his homework for him.
If you tell him what sort of answers to look for, he can learn something by finding them and applying them. If you write the code for him, he'll learn nothing.

commented: Agreed. +14

:)
I agree that this could be a problem, but if that guy is willing to know JAVA he can learn a lot from code by comparing my with his code and learn from his mistakes.If his intend if just to solve his homework, then no searching in this world will make him learn anything.
And please do not rep--; me if you do not like my way of helping someone. Judge my knowledge. Thanks

So was the only thing wrong with mine the way I was trying to output it? Because I'm looking at your code and other than having separate loops for my output I can't tell any major differences.

Look closer, there are a lot of changes that you're apparently not seeing, and they're major ones. Not necessarily the best ones, not necessarily the ones you'd come to, but they are fixes to your code.
Notice also that he didn't address the problem of checking a double for "even" versus "odd". In what way is it meaningful to say that 3.54 is "even" or "odd"?

So was the only thing wrong with mine the way I was trying to output it?

@monarchmk - Can I assume my point has been made?

No, now we had discussion...MrHardRock does not escape with his homework. He is trying to learn something now... :)

@MrHardRock. On a first look, seems like that but...
There are some constructional differences. I'll try to point them in next several lines :)

1) Biggest difference is in a way of filling arrays.
Here is sample how you fill arrays... For example Let say user input 4 numbers: -1 1 2 5

After 1st Number array are

Your way

NEG[0]=-1               
EVEN[0]=null
ODD[0]=null

after 2nd number

NEG[1]=null 
EVEN[1]=null
ODD[1]=1                

after 3rd number

NEG[2]=null
EVEN[2]=2               
ODD[2]=null

After 4th number

NEG[3]=null
EVEN[3]=null
ODD[3]=5            

So at the end you have arrays like this

NEG[0]=-1   EVEN[0]=0   ODD[0]=0
NEG[1]=0    EVEN[1]=0   ODD[1]=1
NEG[2]=0    EVEN[2]=2   ODD[2]=0
NEG[3]=0    EVEN[3]=0   ODD[3]=5

And the Result will be

Negative Numbers are -1 0 0 0 
Even Numbers are 0 0 2 0
Odd numbers are 0 1 0 5

This is not correct. Problem is that when you try to fill arrays, you have one counter (i) and that why you endup with bunch of 0's in all arrays.1 counter does not allow you to fill every array in order. Instead you have chaotic ordering of values in arrays.
You here need to implement counters for each of number types. It is easier and faster way of programming with arrays.

Normal way

NEG[0]=-1
EVEN[0] and ODD[0] are not jet used...

after 2nd number

ODD[0]=1 
EVEN[0] is not jet used.

after 3rd number

EVEN[0]=2

after 4th number

ODD[1]=5

But EVEN[1] and NEG[1] is not used jet.

On this way with counters after 4th number you will have this situation

CNEG=1, CODD=2, CEVEN=1       <- Those are counters and their position after all numbers are processed

Arrays are

NEG[0]=-1   EVEN[0]=2   ODD[0]=1
                ODD[1]=5

Now you notice that there are no zero (0, empty) values in arrays...

after this we are goint to 3 loops to print this and result will be

Negative Numbers are -1
Even numbers are 2
Odd Numbers are 1 5

This is correct. There are no problem with 0's

2) Even if i would not agree with @jon.kiparsky (declaration is programmer choice. We could not know his point. Maybe meaningfull but anyway correct. BTW all decimals are odds.) i must notice that using double just to discover if number is odd/even is not needed.
Declaration in every program language should be taken in consideration. JAVA, C and similar languages are very strict languages so be careful with data types.

3) Declaring scopes. In previous answer i already ask that...
You declare double[] even=new double[11]
but later in code in FOR clause you use
for (int i=1;i<9;i++)

Also there is no point. When you declare Array it starts from 0 till number you declare -1
In example this goes like int[] iarray=new int[3] you will get iarray[0], iarray[1], iarray[2]
for in this case will be
for (int i=0;i<3;i++) and all numbers are correctly alligned.

4) Syntax error
in line 29 of your code you stated if(num/2 != 0). This is math nonsense. Every number divide with 2 is not equal to 2. I assume that you need if (num%2!=0). This is correct and as result gives rest from divide operation.

5) Indentation is a must in every programming. Every group of syntax like FOR, WHILE, DO..WHILE, IF, SWITCH should be indent just to be more readable. Indentation would not slower your code nor makeing it faster, but when you have 1000 lines of code, you will never find which } closes {.

oh, so thank you all for the help.
so what I did was I had one counter that was i, so when i = 0 it would put the number in the 0 position for the array and leave the other two arrays at null, so I need a separate counter for each array. All the other things were things I had an idea on and was planning on working on. Thank you both.

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.