我有一个名为titanic.txt
的文件,我需要读取此文件并创建一个LinkedList来存储所有信息。我似乎无法让它在main中正确打印。当我尝试运行程序时,我得到了一些奇怪的输出。
package project1;
import java.io.File;
import java.util.Scanner;
import project1.LinkedList;
/**
while(line != null){
//Create Object
// split line into 5 pieces first before creating object
//Push that obj into the listObj
//read another line
}
**/
public class Main {
public static void main(String [] args){
//Designate file to a variable
String fileName = "titanic";
//Create the linked list object(named pLinkedList)
LinkedList pLinkedList = new LinkedList();
//Run the try/catch in order to read the file and break up the 5 categories of data.
try (Scanner scanner = new Scanner(new File(fileName))) {
while (scanner.hasNext()){
String [] passenger = scanner.nextLine().split(",");
//System.out.println(passenger[0]); Prints out only the index of the passenger list, etc.
//Create object using Passenger class and the 5 data categories we are analyzing.
Passenger p = new Passenger(passenger[0],passenger[1],passenger[2],passenger[3],passenger[4]);
pLinkedList.insertAtTail(p);
}//While loop
} catch (Exception e) {
System.out.println("Failed to read");
}
pLinkedList.printList();
}
}
package project1;
import project1.ListNode;
public class ListNode {
//Variable data with type Object
private Object data;
//Variable nextNode with type ListNode
private ListNode nextNode;
//ListNode constructor
public ListNode(Object newData){
this.setNextNode(null);
this.setData(newData);
}
//Constructor with two inputs
public ListNode(Object newData, ListNode newNextNode){
this.setNextNode(newNextNode);
this.setData(newData);
}
public ListNode getNextNode() {
return nextNode;
}
public void setNextNode(ListNode nextNode) {
this.nextNode = nextNode;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
package project1;
import project1.EmptyListException;
import project1.ListNode;
public class LinkedList {
private ListNode firstNode;
private ListNode lastNode;
private String listName;
private int size;
public LinkedList(){
listName = "new list";
size = 0;
firstNode = lastNode = null;
}
public int size(){
return size;
}
public boolean isEmpty(){
if(firstNode == null){
return true;
}else{
return false;
}
}
public void insertAtHead(Object item){
if(isEmpty()){
ListNode newNode = new ListNode(item);
firstNode = lastNode = newNode;
}else{
ListNode newNode = new ListNode(item, firstNode);
firstNode = newNode;
}
size++;
}
public Object removeFromHead() throws EmptyListException{
if(isEmpty()){
throw new EmptyListException();
}else{
Object tempData = firstNode.getData();
if(firstNode == lastNode)
firstNode = lastNode = null;
else
firstNode = firstNode.getNextNode();
size--;
return tempData;
}
}
public Object removeFromTail() throws EmptyListException{
if(isEmpty()){
throw new EmptyListException();
}else{
Object tempData;
if(firstNode == lastNode){
tempData = lastNode = null;
return tempData;
}else{
ListNode current = firstNode;
while(current.getNextNode() != lastNode)
current = current.getNextNode();
tempData = lastNode.getData();
lastNode = current;
lastNode.setNextNode(null);
}
size--;
return tempData;
}
}
public void insertAtTail(Object item){
ListNode newNode = new ListNode(item);
if(isEmpty()){
firstNode = lastNode = newNode;
}else{
lastNode.setNextNode(newNode);
lastNode = newNode;
}
size++;
}
public void printList(){
ListNode current = firstNode;
while(current != null){
System.out.println(current.getData().toString());
current = current.getNextNode();
}
}
}
package project1;
public class Passenger {
private String Index; //Passenger number
private String Standing; //Passenger class(1st,2nd,...)
private String Age; //Passenger Age
private String Sex; //passenger gender (Male/Female)
private String Survived; //Passenger status(Alive/Deceased)
public Passenger(String Index,String Standing,String Age,String Sex,String Survived){
this.setIndex(Index);
this.setStanding(Standing);
this.setAge(Age);
this.setSex(Sex);
this.setSurvived(Survived);
}
public String getIndex() {
return Index;
}
public void setIndex(String index) {
Index = index;
}
public String getStanding() {
return Standing;
}
public void setStanding(String standing) {
Standing = standing;
}
public String getAge() {
return Age;
}
public void setAge(String age) {
Age = age;
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
public String getSurvived() {
return Survived;
}
public void setSurvived(String survived) {
Survived = survived;
}
}