Using Arraylists, from reading in a token from a file, how do you take the read-in info and reverse it. So if you have an arraylist of "What is the date?". It would print out "date the is What?".
Please someone help, I can't figure it out... Heres what I have so far (i reversed the two lines but not the words...
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.io.*;
public class ReverseFile2
{
public static void main (String[] arg)
{
Scanner console = new Scanner(System.in);
System.out.println("Enter the input file name: ");
String inputFileName = console.next();
System.out.println("Enter the output file name: ");
String outputFileName = console.next();
ArrayList<String> linesList = new ArrayList<String>();
// Open input and output files
try
{
Scanner in = new Scanner(new File(inputFileName));
PrintStream out = new PrintStream(new File(outputFileName));
// Read a line from one file and write to the other
out.println("Tokenized file is:");
while(in.hasNextLine())//as long as there is more to read
{
String line = in.nextLine();//read next String token
linesList.add(line);
System.out.println("line is " + line);
Scanner in2 = new Scanner(line);
ArrayList<String> wordsList = new ArrayList<String>();
while(in2.hasNext())
{
String token = in2.next();
System.out.println("token is " + token);
wordsList.add(token);
}
}
printBackwards(linesList, out);
//printWordsBackwards(linesList, out);
in.close();
out.close();
}
catch(FileNotFoundException e)
{
System.err.println("Cannot find input file " );
System.exit(1); // abnormal termination status code
}
catch(IOException e)
{
System.err.println("Cannot open input/output file " );
System.exit(2);
}
}
public static void printBackwards(ArrayList aList, PrintStream out)
{
for (int i = aList.size()-1; i >=0; i--)
out.println(aList.get(i));
}
Please someone help, I can't figure it out... Heres what I have so far (i reversed the two lines but not the words...
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.io.*;
public class ReverseFile2
{
public static void main (String[] arg)
{
Scanner console = new Scanner(System.in);
System.out.println("Enter the input file name: ");
String inputFileName = console.next();
System.out.println("Enter the output file name: ");
String outputFileName = console.next();
ArrayList<String> linesList = new ArrayList<String>();
// Open input and output files
try
{
Scanner in = new Scanner(new File(inputFileName));
PrintStream out = new PrintStream(new File(outputFileName));
// Read a line from one file and write to the other
out.println("Tokenized file is:");
while(in.hasNextLine())//as long as there is more to read
{
String line = in.nextLine();//read next String token
linesList.add(line);
System.out.println("line is " + line);
Scanner in2 = new Scanner(line);
ArrayList<String> wordsList = new ArrayList<String>();
while(in2.hasNext())
{
String token = in2.next();
System.out.println("token is " + token);
wordsList.add(token);
}
}
printBackwards(linesList, out);
//printWordsBackwards(linesList, out);
in.close();
out.close();
}
catch(FileNotFoundException e)
{
System.err.println("Cannot find input file " );
System.exit(1); // abnormal termination status code
}
catch(IOException e)
{
System.err.println("Cannot open input/output file " );
System.exit(2);
}
}
public static void printBackwards(ArrayList aList, PrintStream out)
{
for (int i = aList.size()-1; i >=0; i--)
out.println(aList.get(i));
}