0
            if (val1.charAt(0) == 'A' || val1.charAt(0) == 'a')
            {
                System.out.println("Numbers in Ascending Order:");
                    for (int asc : numx) 
                    {   
                        System.out.print (asc + " ");
                    }

            }
            if (val1.charAt(0) == 'D' || val1.charAt(0) == 'd')
            {
                System.out.println("Numbers in Descending Order:");
                    for (int desc : numx) 
                        {   
                            System.out.print (desc + " ");
                        }
            }

        }   
    }
}

Hello im almost finish with this code..

I would like to know how do you sort numbers in Ascending and Descending .. the one in asc/desc?

Redondo Velasco
  • 103
  • 1
  • 8
  • possible duplicate of [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Tom Jun 28 '15 at 02:33
  • About your second part of the question: http://stackoverflow.com/search?q=[java]+array+check+duplicates – Tom Jun 28 '15 at 02:34

4 Answers4

2

If you mean to print all values in numx, I suppose the simplest way would be this:

for (int number : numx) {
  System.out.print(number + " ");
}

That's assuming that you want to use the format you specified in your question (just one space between each item, no other decoration).

Sam Estep
  • 12,974
  • 2
  • 37
  • 75
  • thanks!- another question how would you sort it.. just accept numbers that are not the same? – Redondo Velasco Jun 28 '15 at 02:40
  • @RedondoVelasco You could do that. If you want to sort the array, you could use the [`Arrays.sort`](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#sort-int:A-) function. – Sam Estep Jun 28 '15 at 02:42
  • 1
    @RedondoVelasco if you want 10 unique numbers, add them to a set.. Set does not allow duplicates, so when the size of the set is 10, you have 10 unique numbers. – almightyGOSU Jun 28 '15 at 02:43
  • 1
    @Gosu Yes. However, if you need to keep the original order, you'll either need to fit the [`LinkedHashMap`](http://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) square peg into the round hole of really needing a Set, or handle duplicates manually. – Sam Estep Jun 28 '15 at 02:45
1

If I understand you, could might use Arrays.toString(int[]) like

System.out.println("Accepted numbers are:" + Arrays.toString(numx));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

You can sort the numbers using the following:

Arrays.sort(numx);

Then based on whether you have to print the array in ascending or descending order, print the array in forward or reverse order.

You may use the following method:

String asc = "";
String dsc = "";
for(int num: numx) {
    asc += num + "\n";
    dsc = num + "\n" + dsc;
}
val1.charAt(0) == 'A' || val1.charAt(0) == 'a' ?  System.out.println(asc)
: System.out.println(dsc);
Bhoot
  • 2,614
  • 1
  • 19
  • 36
0
//Use this to sort in ascending order

int max = 0; int temp = 0; int count = 0;

for(int c=0;c<numx.length-1;++c){
    max = numx[c];
    count = c;
    for(int v=c+1;v<numx.length;++v){
        if(numx[v]>max){
            max = numx[v];
            count = c;
        }
    }
    temp = numx[c];
    numx[c] = max;
    numx[count] = temp;
}

//The same goes for descending order but the logic would be (numx[v] < max)