user3275341
user3275341

Reputation: 1

Printing elements of an ArrayList in reverse order

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.

Upvotes: 0

Views: 2227

Answers (2)

ROT13
ROT13

Reputation: 375

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.

Upvotes: 0

rgettman
rgettman

Reputation: 178263

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){

Upvotes: 1

Related Questions