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: reverse

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default reverse

    Create a class that allows the user to select a file and output the contents of the file to the console. Recommend using a text file to display, java files are text files. You may want to put this code in a method to allow you to complete the remainder of this assignment without overwriting your code.
    a. You should ask the user to enter the full path to your file to make certain you are processing the correct file.
    2. Modify your code/add a method to display the contents of the file in reverse order. You do not need to reverse the characters on each line but simply output the lines of the file in reverse order.
    a. Hint: read the content of the file into an ArrayList and then traverse the ArrayList.

    this is what i go so far.

    package classActivity;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;


    public class Select
    {

    public static String enterFilePath()
    {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter path file ");
    String path = input.nextLine();
    return path;
    }

    public static ArrayList<String> readInFile(String path)
    {
    ArrayList<String> lines = new ArrayList<String>();


    File fr = null;
    Scanner inputFile = null;

    try
    {

    fr = new File(path);
    inputFile = new Scanner(fr);
    while (inputFile.hasNextLine())
    {
    lines.add(inputFile.nextLine());
    }

    }
    catch (FileNotFoundException fnf)
    {
    System.out.println(fnf.getMessage());
    }
    catch (Exception e)
    {
    System.out.println(e.getMessage());

    }
    finally
    {
    if (inputFile != null)
    {
    inputFile.close();
    }
    }
    return lines;

    }



    //seperate method Collections.reverse(al);
    public static void Reverse(ArrayList<String> lines)
    {
    Collections.reverse(lines);

    }

    public static void Save(ArrayList<String> lines)
    {
    File file = null;
    PrintWriter out = null;
    try
    {
    file = new File(enterFilePath());
    out = new PrintWriter(file);
    for (String element: lines)
    {
    out.println(lines);
    }
    }
    catch (Exception e)
    {

    }
    finally
    {
    if (out != null)
    {
    out.close();
    }
    }

    }

    public static void main(String[] args)
    {
    String fileName = enterFilePath();
    ArrayList<String> lines = readInFile(fileName);
    Reverse(lines);
    Save(lines);


    }




    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: reverse

    Do you have any specific questions?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: reverse

    Why it didn't reverse or save.

    Quote Originally Posted by Norm View Post
    Do you have any specific questions?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: reverse

    Why it didn't reverse or save.
    What did it do?
    How are you trying to debug the code to find the problem? I use println() statements that print out the values of variables as they are changed and used.

    Please edit the code and wrap it in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    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: reverse

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

Similar Threads

  1. [SOLVED] String reverse
    By Java girl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2014, 11:57 PM
  2. Replies: 9
    Last Post: February 26th, 2013, 11:36 AM
  3. How do I reverse an ArrayList ?
    By BostonBruins in forum Collections and Generics
    Replies: 1
    Last Post: September 15th, 2012, 04:34 PM
  4. Reverse a String
    By Mehwish-S in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 22nd, 2012, 09:03 AM
  5. Reverse Number
    By java1 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 28th, 2009, 10:19 AM

Tags for this Thread