I have a couple array problems that I can\'t for the life of me figure out. I\'v
ID: 3528050 • Letter: I
Question
I have a couple array problems that I can't for the life of me figure out. I've spent countless hours just getting to the point I'm at and even more hours trying to figure out why these two programs still don't work. I amverynew to Java so bear with me...
General guidelines: In order to accomplish this work as quickly as possible, we will not use keyboard or file input. Instead, you may assign the 10 values to your initial arrays in the programs. Using initializer lists to load them with the data types expected for each exercise. Please use methods to do each task. Pass your arrays to the method to complete each task.
1. Write a program that takes 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 integers into an array called negativeList.The program displays the contents of the three arrays after all of the integers have been entered.
Here is my code:
Explanation / Answer
import java.util.*;
import java.io.*;
public class arraysproject1
{
private static int i = 0;
private static int number = 0;
public static void main(String[] args) throws IOException
{
int[] intArray = {13, -2, 10, 7, -15, 20, 51, 87, 22, -90};
sortEvens(intArray);
sortOdds(intArray);
sortNegatives(intArray);
}
public static void sortEvens(int[] intArray)
{
System.out.println("The even numbers are: ");
int[] evenList = new int[5];
int even = 0;
for(i = 0; i < 9; i++){
if(intArray[i] % 2 == 0){
evenList[]=intArray[i]; //you have previously did the reverse way here
even++; // you did not add this part also
}
}
System.out.println(evenList[even]);
}
public static void sortOdds(int[] intArray)
{
System.out.println("The odd numbers are: ");
int[] oddList = new int[5];
int odd = 0;
for(i = 0; i < 9; i++){
if(intArray[i] % 2 != 0){
oddList[odd]=intArray[i]; //you have previously did the reverse way here
odd++; // you did not add this part also
}
}
System.out.println(oddList[odd]);
}
public static void sortNegatives(int[] intArray)
{
System.out.println("The negative numbers are: ");
int[] negativeList = new int[3];
int negative = 0;
for(i = 0; i < 9; i++){
if(intArray[i] < 0){
negativeList[negative]= intArray[i]; //you have previously did the reverse way here
negative+; // you did not add this part also }
}
System.out.println(negativeList[negative]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.