Results 1 to 5 of 5

Thread: reverse

  1. #1
    mikerousse is offline Member
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default reverse

    Hi guys,

    how can I take the reverse of the below file :

    1
    2
    3
    4
    5

    if I am using the following:

    Java Code:
    import java.io.File;
    import java.util.Scanner;
     
     
    public class ReversePartA {
     
        private Scanner x;
         
            public void openFile(){
                try{
                    x=new Scanner(new File("myfile.txt"));
                }
                catch (Exception e){
                        System.out.println("Could not find file");
                                    }
            }
    public void readFile(){
                 
    int loop=0;
                do{
                 
                x.hasNext();
                String a= x.next();
                int b=x.nextInt();
                 
                                         
                System.out.println(b);
                loop++;        
                }while (loop<4);}
             
             
             
    public void closeFile(){
        x.close();
    }
    }

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: reverse

    Take all of the numbers into an array instead of one variable. Then loop through the array backwards and print it out that way.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,351
    Blog Entries
    7
    Rep Power
    20

    Default Re: reverse

    Read entire lines and push them all on a Stack<String>.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    mikerousse is offline Member
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: reverse

    hmmm not sure how I will do this. Any example?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: reverse

    Read the Javadoc for the Stack class. Look at the methods and try them to see how they work.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Reverse a String
    By Mehwish-S in forum New To Java
    Replies: 8
    Last Post: 08-22-2012, 09:23 AM
  2. Reverse a String
    By Mehwish-S in forum NetBeans
    Replies: 1
    Last Post: 08-21-2012, 03:32 PM
  3. Reverse Packer
    By Sean2012 in forum New To Java
    Replies: 2
    Last Post: 04-08-2012, 05:03 PM
  4. Reverse
    By system5634 in forum New To Java
    Replies: 2
    Last Post: 03-26-2012, 09:55 AM
  5. Replies: 10
    Last Post: 03-15-2012, 10:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •