Welcome to Dream.In.Code
Become a Java Expert!

Join 149,581 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,766 people online right now. Registration is fast and FREE... Join Now!




reverse

 
Reply to this topicStart new topic

reverse, help

kick_bot69
12 Oct, 2007 - 05:45 AM
Post #1

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 7


My Contributions
CODE

public Main() {
        
        
      
       List <Character> ListOfNames = new ArrayList <Character>();
      
       Scanner $ = new Scanner(System.in);        
    String line;  
    System.out.print("The Inputted letters: ");                          
      for(int i = 1; i<10; i++)
      {
    
         line = $.nextLine( );
   } // End of for loop...............
        
      
  
    }

/* that is my code guys anyone know where can i put the
* method Collections.reverse(ListOfNames); in this program????
* it has no error but has no result if i add that method what will i do guys.... the output of that is only the inputted characters but it will not reverse

*edit: added code tags, next time please use them, ty - 1lacca
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Reverse
12 Oct, 2007 - 09:17 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
A few things I want to mention here about the boards. Please put all your code in code tags (that is the pound symbol on the editor). This makes it easier for us to look at your code. Secondly, in the future please show the entire method here so that we can see all the lines you are using. If this is all you had, then I suggest you come with perhaps a more completed "attempt". Third, always state your problem clearly and if possible in the form of a question which we can help you answer. Oh and in the future, NEVER use a dollar sign as a variable name. That is so wrong programming style that other programmers will take your head off if they see that in production code.

Ok... so I am going to assume you are attempting to take in a string, reverse the characters using a Character arrayList implementation.

Here you go. Please read the comments to see what I am doing and how I am doing things.

CODE

import java.util.*;


public class reverse {

    public static void main(String args[]) {
        // Setup our Character array (notice this is the reference type of Character here)
        List <Character> ListOfNames = new ArrayList <Character>(10);

        // Prompt the user for a string value and store it.
        Scanner inputScanner = new Scanner(System.in);
        System.out.print("Enter a string value: ");
        String line = inputScanner.nextLine();

        // Show the user what we have input
        System.out.println("The Inputted String: " + line);

        // Loop through the characters to show it in order
        for(int i = 0; i< line.length(); i++)
        {

            // Take each character and through "boxing" we box it up in a Character object.
            System.out.println("Character is: " + line.charAt(i));
            ListOfNames.add(line.charAt(i));
            
        }

        System.out.println();


        // Reverse the character elements
        Collections.reverse(ListOfNames);
        

        // Show characters again to see them in reverse.
        System.out.println("Now to show the reversed version...");
        for (Character c : ListOfNames) {
                System.out.println("Character is: " + c.toString());
        }

    }

}


Notice how I first store each character into the array by taking each char and boxing it up into a Character object. This is called "auto boxing". So now we have a list of Character objects in our arrayList. I go ahead and print them so you can see that they are stored in order. Next I reverse them using the static method reverse in our collections object. I then go back and reprint the list to show you that the characters are now in reverse order.

Enjoy!

"At DIC we be coding ninjas!" decap.gif

This post has been edited by Martyr2: 12 Oct, 2007 - 09:19 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:43PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month