ok so my code displays 0 every even and odd number can you explain to me how to fix this. i have a feeling it is the displayArray.

This is what dispArray should do
create a method called displayArray that takes an array of integers AND an integer as parameters. The integer length will be used to pass the logical size of the array. The method should then display the contents of the array up to the logical size.

import java.util.Scanner;
    public class Arrays1
   {
       public static void main(String[] args) {
      
         Scanner reader = new Scanner(System.in);
         int[] nums = new int[10];
         Scanner sc = new Scanner(System.in);
         int [] even = new int[10];
         int [] odd = new int[10];
         int [] negative = new int[10];
         int evenLogicalSize = 0;
         int oddLogicalSize = 0;
         int negativeLogicalSize = 0;
      	
       
         for(int i = 0; i < 10; i++) {
            int c = i + 1;
            System.out.print("Input number " + c + ":");
            nums[i] = sc.nextInt();
         }
      
         for(int i = 0; i < 10; i++) 
         {
            if(nums[i]% 2.0 == 0 || nums[i] == 0 )
            {
               even[i] = nums[i];
               evenLogicalSize++;      
            } 
            
            else
            {
               odd[i] = nums[i];
               oddLogicalSize++;            
            }
         }
         
             
         
         for(int i = 0; i < 10; i++) 
         {
            if(nums[i] < 0)
            {
               negative[i] = nums[i];
               negativeLogicalSize++;
             
            } 
         
         }
         
         System.out.println();
         System.out.print("Even Numbers: ");
         displayArray(even, evenLogicalSize);
         System.out.println();
         System.out.print("Odd Numbers: ");
         displayArray(odd, oddLogicalSize);
         System.out.println();
         System.out.print("Negative Numbers: ");
         displayArray(negative, negativeLogicalSize);
         
  
      }
      
       public static void displayArray (int [] array, int length){
         for(int k = 0; k<length; k++){
            if(k != length-1){
               System.out.print(array[k] + ", ");
            } else{
               System.out.print(array[k]);
            }
         } 
      
      }
       

   
   }

Recommended Answers

All 13 Replies

for(int i = 0; i < 10; i++) 
         {
            if(nums[i]% 2.0 == 0 || nums[i] == 0 )
            {
               even[evenLogicalSize] = nums[i];
               evenLogicalSize++;      
            } 
 
            else
            {
               odd[oddLogicalSize] = nums[i];
               oddLogicalSize++;            
            }
         }
 
 
 
         for(int i = 0; i < 10; i++) 
         {
            if(nums[i] < 0)
            {
               negative[negativeLogicalSize] = nums[i];
               negativeLogicalSize++;
 
            } 
 
         }

Change this in the code and you'll get the proper answer.

Are you talking about like 5 11 22 ?

You get the zeroes in your even/odd/negative number array because of wrong indexes . Use their respective array indices. There is no problem in displayArray method.

umm i still have an error this is what it outputs.
Even Numbers: 2, 4, 6, 8, 100,
Odd Numbers: 1, 3, 5, 7, 90,
Negative Numbers: 0,

This is my input
Input number 1:1
Input number 2:2
Input number 3:3
Input number 4:4
Input number 5:5
Input number 6:6
Input number 7:7
Input number 8:8
Input number 9:9
Input number 10:10

Show your new code, it looks like you are multiplying the last number by ten (or maybe just adding a zero to the end through some other manner). P.S. I would also suggest not using "2.0" in your modulo statement, just use 2, otherwise you are making that a double operation and it just might not work properly.

Well !! I've executed the program after replacing the codes that i told you to and did get a proper output. Just paste the changed code again , i'll look through it.

Well !! I've executed the program after replacing the codes that i told you to and did get a proper output. Just paste the changed code again , i'll look through it.

And simply pasting a "solution" does not help the OP. Suggest a change or post a small snippet (wihtout using the OPs exact variables) and let the OP struggle through it so that they actually learn something. They learn nothing when someone else does it for them. Not to mention that it is against the policy of this site to do that.

@masijade

I din mean to help without the topic starter understanding the solution. Well as you can see in my earlier replies to him , I've tried to help him understand the mistake too. No offence meant.

import java.util.Scanner;
    public class Arrays1
   {
       public static void main(String[] args) {
      
         Scanner reader = new Scanner(System.in);
         int[] nums = new int[10];
         Scanner sc = new Scanner(System.in);
         int [] even = new int[10];
         int [] odd = new int[10];
         int [] negative = new int[10];
         int evenLogicalSize = 0;
         int oddLogicalSize = 0;
         int negativeLogicalSize = 0;
      	
       
         for(int i = 0; i < 10; i++) {
            int c = i + 1;
            System.out.print("Input number " + c + ":");
            nums[i] = sc.nextInt();
         }
      
         for(int i = 0; i < 10; i++) 
         {
            if(nums[i]% 2 == 0 || nums[i] == 0 )
            {
               even[evenLogicalSize] = nums[i];
               evenLogicalSize++;      
            } 
            
            else
            {
               odd[oddLogicalSize] = nums[i];
               oddLogicalSize++;            
            }
         }
         
             
         
         for(int i = 0; i < 10; i++) 
         {
            if(nums[i] < 0)
            {
               negative[negativeLogicalSize] = nums[i];
               negativeLogicalSize++;
             
            } 
         
         }
         
         System.out.println();
         System.out.print("Even Numbers: ");
         displayArray(even, evenLogicalSize);
         System.out.println();
         System.out.print("Odd Numbers: ");
         displayArray(odd, oddLogicalSize);
         System.out.println();
         System.out.print("Negative Numbers: ");
         displayArray(negative, negativeLogicalSize);
         
  
      }
      
       public static void displayArray (int [] array, int length){
         for(int k = 0; k<=length; k++){
            if(k != length - 1){
               System.out.print(array[k] + ", ");
            } else{
               System.out.print(array[k]);
            }
         } 
      
      }
       

   
   }

This is the new code and for some reason it still adds on the zeros at the end.
This is my input and output.
Input number 1:1
Input number 2:2
Input number 3:3
Input number 4:4
Input number 5:5
Input number 6:6
Input number 7:7
Input number 8:8
Input number 9:9
Input number 10:10

Even Numbers: 2, 4, 6, 8, 100,
Odd Numbers: 1, 3, 5, 7, 90,
Negative Numbers: 0,

I think it might be the end of displayArray

Nope, not unusual at all, especially considering the trailing comma.

Don't use <= in the for loop as the "length" is one larger than the last used index and the number at that (length) index will be 0 (if it does not result in an indexoutofboundsexception will it will if all of the numbers are even, odd, or negative.

Thank you. I just saw that haha.

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.