Home > Mobile >  Array program not running correctly. How do I fix it?
Array program not running correctly. How do I fix it?

Time:01-06

Trying to write a program that asks the a user for 10 integers as input. The program places the even integers into an array called evenList, the odd integers into an array called oddList, and the negative numbers into an array called negativeList. The program displays the contents of the three arrays after all of the integers have been entered.

This is my code:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
    int countNeg = 0;
    int countOdd = 0;
    int countEven = 0;
    int[] list = new int[10];
    System.out.println("Please enter 10 integers:");
    for(int i = 0; i < list.length; i  )
    {
        list[i] = scan.nextInt();
        if(list[i] < 0)
        {
            countNeg  ;
        }
        if(list[i] % 2 == 0 && list[i] > 0)
        {
            countEven  ;
        }
        if(list[i] % 2 == 1 && list[i] > 0)
        {
            countOdd  ;
        }
    }
    int[] oddList = new int[countOdd];
    int[] evenList = new int[countEven];
    int[] negativeList = new int[countNeg];
    for(int i = 0; i < list.length; i  )
    {
        if(list[i] < 0)
        {
            for(int j = 0; j < countNeg; j  )
            {          
                negativeList[j] = list[i];
            }
        }
    }
    for(int i = 0; i < list.length; i  )
    {
        if(list[i] % 2 == 0 && list[i] > 0)
        {
            for(int j = 0; j < countEven; j  )
            {          
                evenList[j] = list[i];
            }
        }
    }
    for(int i = 0; i < list.length; i  )
    {
        if(list[i] % 2 == 1 && list[i] > 0)
        {
            for(int j = 0; j < countOdd; j  )
            {          
                oddList[j] = list[i];
            }
        }
    }

    
    for (int i : negativeList) 
    {
        System.out.print(i   " ");
    }
    
      System.out.println();
    
    for (int i : evenList) 
    {
        System.out.print(i   " ");
    }
    
    System.out.println();
    
    for (int i : oddList) 
    {
        System.out.print(i   " ");
    }
}
}

The program prints the Arrays with the correct amount of values but the wrong numbers. It prints only the last negative, even, or odd number to be input. ex input is 1, 2, 3, 4, 5, 6, -1, -2, -3, -4. For negativeList it prints -4 -4 -4 -4. Im guessing something is wrong in the loops after the arrays are created. Please help!!

CodePudding user response:

you can very much simplify your code with using ArrayList instead of array. For example:

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int length = 10;
        System.out.println("Please enter 10 integers:");

        List<Integer> oddList = new ArrayList<>();
        List<Integer> evenList = new ArrayList<>();
        List<Integer> negativeList = new ArrayList<>();

        for (int i = 0; i < length; i  ) {
            int n = scan.nextInt();
            if (n < 0) {
                negativeList.add(n);
            } else if (n % 2 == 0) {
                evenList.add(n);
            } else {
                oddList.add(n);
            }
        }

        for (int i : negativeList) {
            System.out.print(i   " ");
        }

        System.out.println();

        for (int i : evenList) {
            System.out.print(i   " ");
        }

        System.out.println();

        for (int i : oddList) {
            System.out.print(i   " ");
        }
    }

CodePudding user response:

there are a few issues with the code you provided.

First, in the for loops that initialize the negativeList, evenList, and oddList arrays, you are overwriting the values at each iteration. This means that the final arrays will only contain the last value that was assigned to them. To fix this, you can use a counter variable to keep track of the next index to be filled in each array, like this:

int negCounter = 0;
int evenCounter = 0;
int oddCounter = 0;

for(int i = 0; i < list.length; i  )
{
    if(list[i] < 0)
    {
        negativeList[negCounter] = list[i];
        negCounter  ;
    }
}

for(int i = 0; i < list.length; i  )
{
    if(list[i] % 2 == 0 && list[i] > 0)
    {
        evenList[evenCounter] = list[i];
        evenCounter  ;
    }
}

for(int i = 0; i < list.length; i  )
{
    if(list[i] % 2 == 1 && list[i] > 0)
    {
        oddList[oddCounter] = list[i];
        oddCounter  ;
    }
}

Second, you are not checking if the input values are integers. If the user enters a non-integer value, the program will throw an exception. You can add a check to make sure that the input is an integer like this:

if(scan.hasNextInt())
{
    list[i] = scan.nextInt();
    // ... rest of the code
}
else
{
    System.out.println("Please enter an integer.");
    // If the input is not an integer, discard it and move to the next 
input
    scan.next();
}
  • Related