I am doing a project for Data Structures. I need to have the user input Strings into a doubly linked list using bufferedReader, where -1 terminates the stream and displays the list. Here is what I have so far. Every input I enter is throwing an exception and closing the Stream. Where am I going wrong?
public class Main {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
int count;
String inputString;
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(reader);
DoublyLinkedList list = new DoublyLinkedList();//does this go in the loop?
while ((inputString = in.readLine()) != null)
{
System.out.println("Enter a String, then press enter.");
if (inputString.equals(-1) {
displayList();
} else {
list.insertFirst(inputString);
}
count++
in.close();
}
/**
*
*/
public static void displayList()
{
Node first = null;
Node node = first;
while(node != null){
node.displayNode();
System.out.println("Next Link: " + node.next);
node = node.next;
System.out.println();
}
}
}
Node Class:
public class Node {
public String dData; // data item
public Node next; // next node in list
public Node previous; // previous node in list
/**
*
* @param d
*/
public Node(String d) // constructor
{
dData = d;
}
public void displayNode() // display this link
{
System.out.print(dData + " ");
}
}// end class Node
Doubly-linked-list Class:
public class DoublyLinkedList {
public Node first; // ref to first item
/**
*
*/
public Node last; // ref to last item
public DoublyLinkedList() // constructor
{
first = null; // no items on list yet
last = null;
}
public boolean isEmpty() // true if no links
{
return first == null;
}
public void insertFirst(String dd) // insert at front of list
{
Node newNode = new Node(dd); // make new link
if (isEmpty()) // if empty list,
{
last = newNode; // newLink <-- last
} else {
first.previous = newNode; // newLink <-- old first
}
newNode.next = first; // newLink --> old first
first = newNode; // first --> newLink
}
public void insertLast(String dd) // insert at end of list
{
Node newNode = new Node(dd); // make new link
if (isEmpty()) // if empty list,
{
first = newNode; // first --> newLink
} else {
last.next = newNode; // old last --> newLink
newNode.previous = last; // old last <-- newLink
}
last = newNode; // newLink <-- last
}
public Node deleteFirst() // delete first link
{ // (assumes non-empty list)
Node temp = first;
if (first.next == null) // if only one item
{
last = null; // null <-- last
} else {
first.next.previous = null; // null <-- old next
}
first = first.next; // first --> old next
return temp;
}
public Node deleteLast() // delete last link
{ // (assumes non-empty list)
Node temp = last;
if (first.next == null) // if only one item
{
first = null; // first --> null
} else {
last.previous.next = null; // old previous --> null
}
last = last.previous; // old previous <-- last
return temp;
}
// insert dd just after key
public boolean insertAfter(String key, String dd) { // (assumes non-empty list)
Node current = first; // start at beginning
while (!current.dData.equals(key)) // until match is found,
{
current = current.next; // move to next link
if (current == null) {
return false; // didn’t find it
}
}
Node newNode = new Node(dd); // make new link
if (current == last) // if last link,
{
newNode.next = null; // newLink --> null
last = newNode; // newLink <-- last
} else // not last link,
{
newNode.next = current.next; // newLink --> old next
// newLink <-- old next
current.next.previous = newNode;
}
newNode.previous = current; // old current <-- newLink
current.next = newNode; // old current --> newLink
return true; // found it, did insertion
}
/**
*
* @param key
* @return
*/
public Node deleteKey(String key) // delete item w/ given key
{ // (assumes non-empty list)
Node current = first; // start at beginning
while (!current.dData.equals(key)) // until match is found,
{
current = current.next; // move to next link
if (current == null) {
return null; // didn’t find it
}
}
if (current == first) // found it; first item?
{
first = current.next; // first --> old next
} else // not first
// old previous --> old next
{
current.previous.next = current.next;
}
if (current == last) // last item?
{
last = current.previous; // old previous <-- last
} else // not last
// old previous <-- old next
{
current.next.previous = current.previous;
}
return current; // return value
}
public void displayForward() {
System.out.print("List(first-- > last): ");
Node current = first; // start at beginning
while (current != null) // until end of list,
{
current.displayNode(); // display data
current = current.next; // move to next link
}
System.out.println("");
}
public void displayBackward() {
System.out.print("List(last-- > first): ");
Node current = last; // start at end
while (current != null) // until start of list,
{
current.displayNode(); // display data
current = current.previous; // move to previous link
}
System.out.println("");
}
} // end class DoublyLinkedList
if (inputString = '-1') {
. That's an assignment, not an equality test. Also, single quotes are typically used to identify single characters, not strings (which use double quotes). As for what to do, you can usebreak
to break out of the loop when you read in -1. – Ryan J==
but rather the.equals()
method on theString
object.inputString.equals("-1");
I should point out that this stems to all objects in general, as==
only tests reference and primitive equality. I used String here because that's the object type that is relevant to the question. – Ryan Jin.close()
inside the while-loop? – xrisk