Open the text file and read each line as a String and place that String object into a LinkedList. Print all of the lines in the LinkedList in reverse order.
import java.util.*;
import java.io.*;
public class FileLinkList
{
public static void main(String args[])throws IOException{
String content = new String();
int count=1;
File file = new File("abc.txt");
LinkedList<String> list = new LinkedList<String>();
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()){
content = sc.nextLine();
list.add(content);
}
sc.close();
}catch(FileNotFoundException fnf){
fnf.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
System.out.println("\nProgram terminated Safely...");
}
Collections.reverse(list);
Iterator i = list.iterator();
while (i.hasNext()) {
System.out.print("Node " + (count++) + " : ");
System.out.println(i.next());
}
}
}
Answer from Mavrick on Stack OverflowI was wondering if someone could point me in the right direction for my java project. The purpose of this project is to read a text-file, and input the valid dates (text-file has a list of dates) into a linked list, and then display the linked list using a JFrame.
So far I have a class called Date212 whose constructor has a string parameter. The purpose of this constructor is to read the text-file, and parse the Date which is in a String format into 3 integers variable, and do some error checking.
Code for this class can be found here.
My professor instructed us to created another class, DateNode, for the linked class which has two nodes. One to store the dates from ABC, and another pointer to point to the next node. We were also instructed to create a constructor that takes a date as a parameter.
public class DateNode
protected Date212 temp;
protected DateNode next;
public DateNode(Date212 anotherTemp)
temp = anotherTemp;
next = null;Next we were told to create another linked list, DateList, and modify so that the data type in the nodes is ABC, and create a no-argument constructor that creates an empty list with first and last pointing to an empty head node, and length = 0. And a method myAdd that adds the new node to the end of the list.
public class DateList {
private DateNode first;
private DateNode last;
private int length;
public DateList()
DateNode x = new DateNode(null)
first = x;
last = x;
length = 0;
public void myAdd(DateNode lastTemp)
DateNode firstTemp = new DateNode(lastTemp)
last.next = firstTemp;
last = firstTemp;
length++Now here's where I am confused. We are supposed to call upon these classes in my main class, Main, which I have no idea how to call. So far in my Main, I have:
public static void main(String args[]) {
Date212 xyz = new Date212("text.txt")
DateList myList = new DateList()
// Trying to add the data from ABC into my linked list.
myList.myAdd(xyz)After that I am completely lost as to what I am supposed to do. Does my above code actually add the data from the text-file into my linked list?
Edit: I am not worried about printing it out on a JFrame at the moment, because I am confident that I can do that part easily at the end.