please help me about this program...i dont how to sort the even numbers inputted.

import java.io.*;
import java.util.*;
public class mp5{
    public static void main(String[ ]args) throws IOException{
        BufferedReader br=new BufferedReader (new InputStreamReader (System.in)) ;
        int num=0,sum=0;
        String snum;
        System.out.print ("Enter size of Array: ");
        snum=br.readLine( );
        num=Integer. parseInt( snum);
        if (num>10){
            System.out.print ("Enter size not more than 10!!");
        else {
            String st,sp1="",sp2= " ";
            int arr[];
            arr=new int [num];
                for (int i=0;i<num;i++){
                    System.out.print ("Enter element " + i + ": ");
                    st=br.readLine( );
                    arr[i]=Integer. parseInt( st);
                    sum=sum + arr[i];
                    sp1= sp1 + st + sp2;

                System.out.println(Arrays.toString( arr));

                System.out.println ("The elements of the array are " + sp1);
                System.out.println ("The sum of the elements is " + sum);


7 Years
Discussion Span
Last Post by rue64ja

Ok, so if you want to sort the even numbers inputted, then I think there are two things that should be considered. First, you need to know whether an inputted number is even, and second you will have to know how large the number is if it isn't the only even number.

if( array[i] % 2 != 0) {/* The number is not even */}
        else {/* The number is even */}

That is one way you could check for an even number. I don't think the java API has any predefined methods for sorting even numbers in an array, it seems like that is the purpose of the assignment. You have to define your own or do it in your main method if you like. Sorting a one dimensional array is pretty straight forward though so you shouldn't have to think very long.

Here is some syntax for sorting an array yourself.

if(/* An element is larger or smaller than another (which ever you prefer) */) 
          /* Switch the positions of the two elements. Note that this isn't as trivial as just swapping the two postions. 
           * You will see, if you don't already notice. */
        else {/* Do nothing.  Actually this else clause isn't even needed. */}

I haven't given any detailed code because it seems like this is an assignment, and you should think about it. Arrays are fundamental in Java.

if this helps would you mind giving me a +up?

Thanks & Good luck!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.