0

I have been given a text file which reads:

aaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbb
ccccccccccccccccccc
ddddddddddddddddddd

and I have to make the program display it in the this order:

ddddddddddddddddddd
ccccccccccccccccccc
bbbbbbbbbbbbbbbbbbb
aaaaaaaaaaaaaaaaaaa

So far this is my code:

public class LineReverserMain {

    public static void main(String[] args) throws FileNotFoundException
    {

        int lineCount = 0;
        ArrayList <String> LinesArray = new ArrayList<String>( );            

        Scanner in = new Scanner(System.in);
        System.out.print("Please enter the file name: ");
        String filename = in.next();
        File file = new File(filename);           

        Scanner inFile = new Scanner(file);           

        while (inFile.hasNextLine()){                 
            lineCount += 1;                
            String CurrentLine = inFile.nextLine();              
            LinesArray.add(CurrentLine);
        }            

        System.out.println(LinesArray);
        System.out.println(lineCount + " lines");

        for(int linesCount = lineCount; linesCount>=0; linesCount = linesCount - 1){
            System.out.println(LinesArray.get(linesCount));
        }            
    }
}

But this doesn't seem to work.

2
  • It wont display the lines in reverse order Commented Feb 18, 2014 at 23:04
  • How does it display them - or dos it error? If it errors, what is the error and what does that teach you? Remember that an n-element list starts at element 0 and ends with element (n-1). Commented Feb 18, 2014 at 23:19

2 Answers 2

1

The problem is your for loop at the end. At this time, lineCount is how many lines you have, but a valid index for your ArrayList is between 0 and lineCount - 1, inclusive. You must be getting an IndexOutOfBoundsException.

Start your linesCount variable one below lineCount. Change

for(int linesCount = lineCount; linesCount>=0; linesCount = linesCount - 1){

to

for(int linesCount = lineCount - 1; linesCount>=0; linesCount = linesCount - 1){
Sign up to request clarification or add additional context in comments.

1 Comment

if this answer solved the problem, remember to mark the question answered.
0

It's a classical problem you can solve with a stack:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;

public class LineReverserMain {

    public static void main(String[] args) throws IOException {

                    // Linecounter and stack initialization
        int lineCount = 0;
        Stack<String> stack = new Stack<String>();

                    // Scaner and Filereader initialization
        Scanner s = new Scanner(System.in);
        System.out.print("Please enter the file name: ");
        String filename = s.next();
        File file = new File(filename);

                    // Push every read line onto the stack
        BufferedReader in = new BufferedReader(new FileReader(file));
        while (in.ready()) {
            stack.push(in.readLine());
            lineCount++;
        }

                    // While the stack isn't empty get the top most element
        while (!stack.isEmpty())
            System.out.println(stack.pop());
        System.out.println(lineCount);

                    // Close the Scanner and FileReader
        s.close();
        in.close();
    }
}

The stack hast a FILO structure, so you can just save String on it and pop them afterwards and get the correct order. Maybe you are interested in this shorter solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.