0

This code print the array (1, 7, 8, 22, 37, 55, 80) the way it is without calculating its evens.

The output that I want (8, 22, 80).

The output that I get (1, 7, 8, 22, 37, 55, 80).

///The getEvens() method
public static int[] getEvens(int a[]) {

    int myEvens = 0;

    for (int i = 0; i < a.length; i++) {
      if (a[i] % 2 == 0) {
         myEvens++;
      }    
    }

    Arrays.sort(a);

    return a;
}

\\\\The main method
public static void main(String args[]) {

    int [] getEvens;
    int [] myArray = {1,8,80,22,55,37,7};
    int [] evenResult = getEvens(myArray);

    System.out.print("\nThe even numbers in the array(" +    Arrays.toString(myArray)+ ") are:{ " + Arrays.toString(evenResult)+ "}.");

}
Makoto
  • 104,088
  • 27
  • 192
  • 230
Bret Dun
  • 33
  • 1
  • 8

5 Answers5

2

One of the issues is that you are not returning anything from the getEvens() method, so your second toString will not give you what you need. Also you increment myEvens, but this value is functioning as a counter. You need to save the even value that is in the array.

You can do this with ArrayList easily:

//import ArrayList
import java.util.ArrayList;
//before method
List<Integer> evens = new ArrayList<Integer>();
//in the for-loop
evens.add(a[i]);

See more on ArrayLists here.

If you want to continue working with arrays, you could create a new array of length myEvens, run through your original loop again, and add each even value to your new array. However, to do this, you would need a way to keep track of which index you are on in your new array.

Sorting algorithms are available online. array.sort() works for array values. ArrayList implementation of sort is given here.

Community
  • 1
  • 1
  • error i get: ** symbol : class List location: class Method2 List evens = new ArrayList(); ^ 1 error ** – Bret Dun Oct 10 '16 at 17:57
  • Probably worth mentioning that the static method [`Collections.sort`](https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List)) is the equivalent method when working with lists. – nbrooks Oct 10 '16 at 17:57
  • if u don't mind, please put it in my code and show it here. I'm trying so hard but still getting errors – Bret Dun Oct 10 '16 at 18:51
  • import java.util.*; ArrayList evens = new ArrayList(); public static List getEvens(int[]a){ for(int i:a){ if(i%2) == 0{ evens.add(i); } Collections.sort(evens); } return evens; } – Sondering Narcissist Oct 11 '16 at 22:24
0

Right now, all you're doing is counting the number of evens in your array, which is fine if that's all you want to do. If you actually want them to be extracted into their own array, that's trickier since arrays aren't dynamic.

You have two options and I would prefer the latter:

  1. Create a List<Integer> and store the values there, then extract them into an array (if you so desire):

    // Declare this list outside of the loop
    List<Integer> evens = new ArrayList<>();
    if(a[i] % 2 == 0) {
        evens.add(a[i]);
    }
    return evens.toArray(new int[0]);
    
  2. Use Arrays.stream to build a stream and filter out an array with only the even values:

    // Just return this explicitly
    return Arrays.stream(a).filter(i -> i % 2 == 0)
                           .toArray();
    
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Depends on your errors. I listed my suggestions here as a guideline. The latter suggestion will only work with Java 8, so be sure you have that installed. – Makoto Oct 10 '16 at 17:55
  • error i get: **bold** symbol : class List location: class Method2 List evens = new ArrayList(); ^ 1 error – Bret Dun Oct 10 '16 at 17:56
  • You should import `java.util.List` and `java.util.ArrayList` into your class. – Makoto Oct 10 '16 at 17:59
  • if u don't mind, please put it in my code and show it here. I'm trying so hard but still getting errors(The first option) – Bret Dun Oct 10 '16 at 18:52
  • I do mind; I've only elected to give you a hint here. It is entirely up to you to understand how to incorporate it in your code. – Makoto Oct 10 '16 at 19:01
0

Since nobody mentioned it, here is a concise version for Java 8:

public static int[] getEvens(int a[]) {
    Arrays.stream(a).filter(i -> i % 2 == 0).toArray();
}
khachik
  • 28,112
  • 9
  • 59
  • 94
-2

You're actually just sorting and returning the same array that was passed into your method. In your if-statement where you're counting "myEvens" you could add a[i] to a new array and then return that one instead of a (which is the input-parameter).

TenaciousG
  • 139
  • 8
-2

Your code is trying to count the number of evens you have in the array. As @jeff carey said you can create new array and evens to them. Following is the code snippet for you

   public static List<int> getEvens(int []  a) {

int myEvens = 0;
        int [] evenArray=new int[3];
        List<int> evenList=new List<int>();
        int length=a.Length;
        int index=0;

for (int i = 0; i <length; i++) {

  if (a[i] % 2 == 0) {

     myEvens++;
     evenList.Add(a[i]);
  }    

}

 Array.Sort(a);

 return evenList;
}

Hope this helps

Geeky
  • 7,420
  • 2
  • 24
  • 50