What is the name of the data file?
> input.txt
Finished reading the file.
Next command
> show line
Exception in thread "main" java.lang.NullPointerException
at CLLOfRiders.showWaiting(CLLOfRiders.java:35)
at Driver2.commands(Driver2.java:69)
at Driver2.userPrompt(Driver2.java:61)
at Driver2.main(Driver2.java:18)
Driver2.java
import java.util.*;
import java.io.*;
public class Driver2 {
static String fileName;
static CLLOfRiders riders = new CLLOfRiders();
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
askFile();
userPrompt();
}
public static void readFile(String fileName) {
try {
Scanner file = new Scanner(new File(fileName));
Rider info;
while (file.hasNext()) {
String tempD = file.nextLine();
String delims = "[ - ]";
String[] tokens = tempD.split(delims);
String name = tokens[0];
char pass = tokens[0].charAt(0);
info = new Rider(name, pass);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void askFile() {
Scanner input = new Scanner(System.in);
boolean check = false;
while (check == false) {
try {
System.out.println("What is the name of the data file?");
System.out.print("> ");
fileName = input.nextLine();
readFile(fileName);
check = true;
System.out.println("Finished reading the file.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void userPrompt() {
String command;
do {
System.out.println("\nNext command");
System.out.print("> ");
command = keyboard.nextLine();
commands(command);
} while (!command.equals("quit"));
}
public static void commands(String command) {
switch (command) {
case "add rider": addRider();
break;
case "show line": System.out.println(riders.showWaiting());
break;
}
}
public static void addRider() {
System.out.println("Name\n");
System.out.print("> ");
String name = keyboard.nextLine();
System.out.println("SpeedPass\n");
System.out.print("> ");
char pass = keyboard.nextLine().charAt(0);
Rider person = new Rider(name, pass);
riders.addRider(person);
}
}
CLLOfRiders
public class CLLOfRiders {
private Node tail;
public CLLOfRiders () {
tail = null;
tail.setNext(tail);
}
public void addRider(Rider r) {
Node n = new Node(r);
if (n.getData().getSpeedPassStatus() == 'Y') {
n.setNext(tail.getNext());
tail.setNext(tail);
} else {
Node tmp = tail;
while (tmp.getNext() != tail) {
tmp = tmp.getNext();
}
tmp.setNext(n);
n.setNext(tail);
}
}
public String showWaiting() {
Node tmp;
Node end = tail.getNext();
String result = "";
for(tmp=end; tmp.getNext()!=end; tmp=tmp.getNext()) {
result = result + tmp.getData().getName() + "\n";
}
return result;
}
public void processRiders(int queue) {
for (int n = 0; n < queue; n++) {
if (tail.getNext() != null || tail.getNext().getNext() != null)
tail.setNext(tail.getNext().getNext());
}
}
}
Node
public class Node {
private Rider r;
private Node next;
public Node (Rider data) {
r = data;
next = null;
}
public void setNext (Node nd) {
next = nd;
}
public Node getNext () {
return next;
}
public Rider getData() {
return r;
}
}
Rider
public class Rider {
private String name;
private char speedPassStatus;
public Rider() {
name = "Unknown";
speedPassStatus = 'N';
}
public Rider(String name, char speedPassStatus)
{
this.name = name;
this.speedPassStatus = speedPassStatus;
}
public String getName() {
return name;
}
public char getSpeedPassStatus() {
return speedPassStatus;
}
public void setSpeedPassStatus(char status) {
speedPassStatus = status;
}
}
This post has been edited by lilVaratep: 08 October 2012 - 07:28 PM

New Topic/Question
Reply



MultiQuote



|