Hello StackOverFlow Community. The program I wrote has initialized integers to be added onto when an even, odd, or negative number is detected. It is then supposed to then take those numbers and create arrays at the length of those integers. The issue then becomes grabbing the numbers that are Even, Odd, or Neither and putting them in an array before those arrays are initialized.
How can I approach this problem? If I create these arrays at the top, then they will all be the length of 0 which is not good.
Here is the program:
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int [] twelveInt = new int [12];
int countEven = 0;
int countOdd = 0;
int countNeg = 0;
int i = 0;
for(i = 0; i < twelveInt.length; i ){
System.out.println("Enter the #" (i 1) " integer.");
twelveInt [i] = in.nextInt();
if (twelveInt[i] % 2 == 0){
countEven ;
}
if (twelveInt[i] % 2 != 0){
countOdd ;
}
if (twelveInt[i] < 0){
countNeg ;
}
}
int [] evenList = new int [countEven];
int [] oddList = new int [countOdd];
int [] negativeList = new int [countNeg];
System.out.println("Here are the Even numbers you entered");
System.out.println(Arrays.toString(evenList));
System.out.println("Here are the Odd numbers you entered");
System.out.println(Arrays.toString(oddList));
System.out.println("Here are the Negative numbers you entered");
System.out.println(Arrays.toString(negativeList));
Any advice on grabbing these integers is greatly appreciated.
CodePudding user response:
Use Arrays.copyOf()
init your arrays
int[] odds = new int[0];
when you add to the array
odds = Arrays.copyOf(odds, odds.length 1);
odds[odds.length-1] = i;
CodePudding user response:
Use ArrayList
instead because you can manipulate the size unlike an array.
You need to grab them inside the for-loop but declare those arraylists before it like so:
List<int> evenList = new ArrayList<>();
List<int> oddList = new ArrayList<>();
List<int> negativeList = new ArrayList<>();
for(i = 0; i < twelveInt.length; i ){
System.out.println("Enter the #" (i 1) " integer.");
twelveInt [i] = in.nextInt();
if (twelveInt[i] % 2 == 0){
countEven ;
evenList.add(i); //here you add to the evenList the number i
}
if (twelveInt[i] % 2 != 0){
countOdd ;
oddList.add(i); //same here for oddList
}
if (twelveInt[i] < 0){
countNeg ;
negativeList.add(i);// same here for negList
}
}
CodePudding user response:
The easy to understand fix that doesn't use List
s (presuming that your tutorial specifies this) is two loops; one to count, one to populate the results:
// delete the line "int i = 0;" !
for (int i = 0; i < twelveInt.length; i ) {
System.out.println("Enter the #" (i 1) " integer.");
twelveInt [i] = in.nextInt();
if (twelveInt[i] % 2 == 0){
countEven ;
}
if (twelveInt[i] % 2 != 0){
countOdd ;
}
if (twelveInt[i] < 0){
countNeg ;
}
}
// declare and initialize result arrays with correct length
int [] evens = new int [countEven];
int [] odds = new int [countOdd];
int [] negatives = new int [countNeg];
// reset counters - use them for populating the result arrays
countEven = 0;
countOdd = 0;
countNeg = 0;
// same logic, this time writing to result arays
for (int i : twelveInt) {
if (i % 2 == 0){
evens[countEven ] = i;
}
if (i % 2 != 0){
odds[countOdd ] = i;
}
if (i < 0){
negatives[countNeg ] = i;
}
}
Note use of foreach loop in the second loop.