Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Coding the Fun Facts extra

trying out the Coding the Fun Facts extra credit.

Got stuck on how to have a String list the array elements in increasing or decreasing order (or even how to have a String include more than one array element)

So far I have:

final TextView increasing = (TextView) findViewById(R.id.increasingView);

    final TextView decreasing = (TextView) findViewById(R.id.decreasingView);

    String[] number = {"1","2","3","4","5","6","7","8","9","0"};

    String incr = number[0];  //wrong part
    String decr = number[9]; //wrong part

    increasing.setText(incr);
    decreasing.setText(decr);

oh, the project is: Create a new project and add two TextViews to the screen. Create an array of numbers and see if you can display the numbers in order in the first TextView and then reversed in the second TextView.

2 Answers

The main clues are : 1- create an array of ints instead of strings. 2- look into the following method in documentation Arrays.sort(int[]) 3- look into another method for converting the int array into string Arrays.toString(int[])

final TextView increasing = (TextView) findViewById(R.id.increasingView); final TextView decreasing = (TextView) findViewById(R.id.decreasingView);

    String[] number = {"6","3","8","1","0","4","7","9","2","5"};

    for (String incr : number) {
        Arrays.sort(number);
        increasing.setText(incr);
    }

    for (String decr : number) {
        Arrays.sort(number, Collections.reverseOrder());
        decreasing.setText(decr);
    }

actually (sort of) did it with this in strings (as you can see, the array's not in order, so it ordered it), but (I think) it printed it out on top of itself, so the result was:

9

0

but i'll give it another go trying using an int array instead, sorting it, converting to string ..as you say.

Following your approach, you can sort the string[] array "number" only once outside the for loop. (No need to sort it every time in the loop). Create an empty string before the for loop. Then, in the for loop just concatenate each number onto the string you created above. Then after the for loop ends, you can setText on the TextView with the whole string.

I've been reviewing all the courses, because i found myself not quite understanding what I was coding (was able to pass the challenges, by comparing code that was just written whilst following the vids). Was able to figure this out much more easily this time 'round (and the exercise did specify the array had to be ints) ...so:

final int[] numbersArray = { 5,7,3,0,1,2,8,9,4,6 };

    final TextView increasingNumbers = (TextView)findViewById(R.id.increasingView);
    final TextView decreasingNumbers = (TextView)findViewById(R.id.decreasingView);

    Arrays.sort(numbersArray);


    for (int a = 0; a < numbersArray.length; a++) {
        increasingNumbers.append(String.valueOf(numbersArray[a]));
    }

    for (int b = numbersArray.length - 1; b > -1; b--) {
        decreasingNumbers.append(String.valueOf(numbersArray[b]));
    }