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 10 of 10

Thread: How to reverse a string array ?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default How to reverse a string array ?

    Hello again. I have an array of friends names and I want to print it in reverse. I know this question seems a bit lazy, but i really have tried casting the string as ints and even using lazy = String,valueOf(i) etc but No luck so far. Thanks

     
    //FriendList.java
     
    public class FriendList  {
     
      public static void main (String[] args)  {
     
      String [] friends = {"danny", "vic", "Laura", "Mike", "Jenny", "Asrath"};
     
      System.out.println ("My first 6 best friends are: ");
     
      for (String lazy : friends)
          System.out.println (lazy + " ");
     
      System.out.println ("Now them in reverse");
     
      //Problems begin here
     
      for (String lazy = friends.length; lazy >= 0; lazy--)
         System.out.println (lazy + " ");
     
      }
    }


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

    Default Re: How to reverse a string array ?

    What should the reverse output look like? I'm not sure what you mean yet.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to reverse a string array ?

    The first for loop prints : Danny, vic , laura, mike , jenny, asrath.
    I want the second for loop to print them in reverse like: asrath, jenny mike, laura, vic. Danny.

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

    Default Re: How to reverse a string array ?

    The first loop prints the array elements from 0 through 5. The second loop prints the array elements from 5 through 0. Try to code that.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to reverse a string array ?

    //FriendList.java
     
    public class FriendList  {
     
      public static void main (String[] args)  {
     
      String [] friends = {"danny", "vic", "Laura", "Mike", "Jenny", "Asrath"};
     
      System.out.println ("My first 6 best friends are: ");
     
      for (String lazy : friends)
          System.out.println (lazy + " ");
     
      System.out.println ("Now them in reverse");
     
      //Problems begin here
     
      System.out.println (friends[5]);
      System.out.println (friends[4]);
      System.out.println (friends[3]);
      System.out.println (friends[2]);
      System.out.println (friends[1]);
      System.out.println (friends[0]);
     
      }
    }
    I can't figure out how to do that I can only write them out manually. How do you move down through the array elements ?

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

    Default Re: How to reverse a string array ?

    The enhanced for loop is a great tool, and I'm impressed that you're using it already, but unfortunately, it only iterates the collection in the forward direction. You'll have to write a loop, usually the standard for loop, to iterate the array in the reverse direction. Can you do that? Can you write a standard for loop in the forward direction? If so, start with that, and think about how you'd write it to go in the reverse direction. If you need help, show what you can do and ask for help.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    craigjlner (November 9th, 2013)

  8. #7
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to reverse a string array ?

    Well I did it Thanks Greg.
    //FriendList.java
     
    public class FriendList  {
     
      public static void main (String[] args)  {
     
      String [] friends = {"danny", "vic", "Laura", "Mike", "Jenny", "Asrath"};
     
      System.out.println ("My first 6 best friends are: ");
     
      for (int total = 0; total <= 5; total++)  {
          System.out.println (friends[total]);
      }
     
     
      System.out.println ("Now in reverse");
     
      for ( int total = 5; total >= 0; total--)  {
          System.out.println (friends[total]);
      }
      }
    }

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

    Default Re: How to reverse a string array ?

    Good job! Glad to help, but I didn't do much.

  10. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to reverse a string array ?

    I'm impressed. There are many people new to programming that are unable to get the logic of printing stuff in reverse order. Going forward no probs. Going backwards nah too difficult.

    Well done!
    Improving the world one idiot at a time!

  11. #10
    Junior Member
    Join Date
    Oct 2013
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to reverse a string array ?

    What you are doing is going with a wrong for loop initialization. It will be like this:

    public class FriendList  {
     
      public static void main (String[] args)  {
     
      String [] friends = {"danny", "vic", "Laura", "Mike", "Jenny", "Asrath"};
     
      System.out.println ("My first 6 best friends are: ");
     
      for (String lazy : friends)
          System.out.print(lazy + " ");
     System.out.println();
      System.out.println ("Now them in reverse");
     
      //Problems begin here
     
      for (int lazy = (friends.length - 1); lazy >= 0; lazy--)
         System.out.print(friends[lazy] + " ");
     
      }
    }

Similar Threads

  1. Replies: 9
    Last Post: February 26th, 2013, 11:36 AM
  2. Reverse a String
    By Mehwish-S in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 22nd, 2012, 09:03 AM
  3. Reverse String
    By WantHelp in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 5th, 2011, 06:14 AM
  4. How to reverse a string, skiping numbers , and ' ?
    By mlotfi in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 26th, 2011, 05:13 AM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM