Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: two for one

  1. #1
    Junior Member ZaneDarklace's Avatar
    Join Date
    Feb 2014
    Location
    Lawton, OK
    Posts
    15
    My Mood
    Lonely
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default two for one

    Hello. I am working on this and it asks for me to print a series of names in ascending order then descending order. This is what the projects asks:
    Call both your project and class AscendDescend. The first column should be in ascending order and the second in
    descending order. The output should appear as below (Be sure to include the headers):
    Ascend Descend
    Agnes Thomas
    Alfred Mary
    Alvin Lee
    Bernard Herman
    Bill Ezra
    Ezra Bill
    Herman Bernard
    Lee Alvin
    Mary Alfred
    Thomas Agnes

    This is my code so far:
    import java.util.*;
    public class twoforone
    {
      public static void main(String args[])
      {
        String theArray[] = {"Bill", "Mary", "Lee", "Agnes", "Alfred", "Thomas", "Alvin", "Bernard", "Ezra", "Herman"};
        for(int j = 0; j < theArray.length; j++)
        {
          Arrays.sort(theArray);
          System.out.println(theArray[j] + " ");
        }
        System.out.println(" ");
        {
          for(int m = 0; m < theArray.length; m++)
          {
            Arrays.sort(theArray);
          System.out.println(theArray[m] + " ");
          }
        }
      }
    }

    I cannot get it to print in descending order. Please send me feed back on this.
    Rock On

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: two for one

    To create a for loop that begins at the end of an array and iterates it to the beginning, do something like:

    for ( j = myArray.length - 1 ; j > -1 ; j-- )

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: two for one

    Note
    You used Arrays.sort(arg) method repeatedly since that statement is inside your loop
    for(int j = 0; j < theArray.length; j++)
        {
          Arrays.sort(theArray);
          System.out.println(theArray[j] + " ");
        }

    but actually you can do it once. Put the Arrays.sort(args) outside the loop, and you will
    notice that the elements of that array was sorted after that statement. The reason
    of that is because of being mutable of Array.

  4. #4
    Junior Member ZaneDarklace's Avatar
    Join Date
    Feb 2014
    Location
    Lawton, OK
    Posts
    15
    My Mood
    Lonely
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: two for one

    the m array is actually the descending array. i just couldnt figure out how to make it descending.

    --- Update ---

    i have it to where it descends in order but i have yet to make it to where the arrays are side by side.
    import java.util.*;
    public class twoforone
    {
      public static void main(String args[])
      {
        String theArray[] = {"Bill", "Mary", "Lee", "Agnes", "Alfred", "Thomas", "Alvin", "Bernard", "Ezra", "Herman"};
        System.out.println("Ascending");
        System.out.println(" ");
        for(int j = 0; j < theArray.length; j++)
        { 
          Arrays.sort(theArray);
          System.out.println(theArray[j]);
        }
        System.out.println(" ");
        System.out.println("Descending");
        System.out.println(" ");
        for (int m = theArray.length - 1 ; m > -1 ; m--)
        {
          System.out.println(theArray[m]);
        }
     
      }
    }

    It prints this:
    Ascending
     
    Agnes
    Alfred
    Alvin
    Bernard
    Bill
    Ezra
    Herman
    Lee
    Mary
    Thomas
     
    Descending
     
    Thomas
    Mary
    Lee
    Herman
    Ezra
    Bill
    Bernard
    Alvin
    Alfred
    Agnes

    can someone help me code it to where the arrays are side by side?
    Rock On

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: two for one

    Be creative. You only need one array sorted in order. In one column you print the array in ascending order, in the second column you print the array in descending order. One loop can provide an index delta. In the first column, the index delta is from the beginning. In the second column, the index delta is from the end.

Tags for this Thread