I have a linked list and I am trying to make a function that removes charms above a certain price. Lets say I have 4 charms and two of them cost $1.25 how would I remove them?
I got my function to traverse the Linked list and remove the charms above the price but I get an error saying it cannot set the node because it is null
class Main {
public static void main(String[] args) {
CharmList Bracelet = new CharmList();
Bracelet.add(new Charm("YOLO", 1.25));
Bracelet.add(new Charm("Donald Duck", 1.25));
Bracelet.add(new Charm("Hello Kitty", 3.25));
Bracelet.add(new Charm("Drums", 4.25));
System.out.print(Bracelet);
System.out.println(Bracelet.total());
System.out.println(Bracelet.findPrice("YOLO"));
Bracelet.remove(1.25);
System.out.println(Bracelet);
}
}
void remove(double price) {
try {
CharmNode back = null;
CharmNode current = list;
boolean found = false;
while (current != null && !found) {
if(current.getCharm().getPrice() == price) {
found = true;
} else {
back = current;
current = current.getNextNode();
}
if (found) {
back.setNode(current.getNextNode());
current.setNode(null);
}
}
} catch(NullPointerException e) {
e.printStackTrace();
}
}
public class CharmList {
private CharmNode list;
CharmList(){
list = null;
}
void add(Charm c) {
try {
CharmNode temp = new CharmNode(c);
CharmNode current = list;
CharmNode back = null;
boolean found = false;
while((current != null) && (!found)) {
if(temp.getCharm().getName().compareTo(current.getCharm().getName())<0) {
found = true;
} else {
back = current;
current = current.getNextNode();
}
}
temp.setNode(current);
if(back == null) {
list = temp;
}else {
back.setNode(temp);
}
} catch(NullPointerException e) {
e.printStackTrace();
}
}
void remove(double price) {
try {
CharmNode back = null;
CharmNode current = list;
boolean found = false;
while (current != null && !found) {
if(current.getCharm().getPrice() == price) {
found = true;
} else {
back = current;
current = current.getNextNode();
}
if (found) {
back.setNode(current.getNextNode());
current.setNode(null);
}
}
} catch(NullPointerException e) {
e.printStackTrace();
}
}
public double findPrice(String name) {
try {
CharmNode back = null;
CharmNode current = list;
boolean found = false;
while (current != null && !found) {
if (current.getCharm().getName().compareToIgnoreCase(name) == 0) {
found = true;
} else {
back = current;
current = current.getNextNode();
}
if (found) {
return current.getCharm().getPrice();
}
}
} catch(NullPointerException e) {
e.printStackTrace();
}
return 0.0;
}
#Override
public String toString() {
String result = "";
CharmNode current = list;
while(current != null) {
result += current.getCharm().getName() + ", ";
current = current.next;
}
if (list == null) {
System.out.println("<No Charms>");
}
return result;
}
public double total() {
double cost = 0.0;
CharmNode current = list;
while(current != null) {
cost += current.getCharm().getPrice();
current = current.next;
}
return cost;
}
}
public class CharmNode {
Charm charm;
CharmNode next;
CharmNode(Charm c){
charm = c;
next = null;
}
public Charm getCharm(){
return charm;
}
public CharmNode getNextNode() {
return next;
}
void setNode(CharmNode c) {
next = c;
}
}
Your issue is that the first node is the one you are trying to remove, which means back is null hence the NullPointerException. You need to account for this with a special condition. So if the node to be removed is the first node then the next node needs to become the new first node:
void remove(double price) {
CharmNode prev = null;
CharmNode current = list; // list is the first node (I would rename it)
while (current != null) {
// found first instance
if (current.getCharm().getPrice() == price) {
// current is the first
if (prev == null) {
list = current.getNextNode()) // set the new first node
current.setNode(null)
} else {
back.setNode(current.getNextNode());
current.setNode(null);
}
break;
}
back = current;
current = current.getNextNode();
}
}
The first CharmNode price matches 1.25, so it calls back.setNode() while back is null.
I have to create and use LinkedList (implement from scratch) to work with the Book Library Management Program. I have 3 files which contain different classes, 3 main classes in 3 files are the BookList - which is a list of Books, ReaderList - which stores the list of readers and LendingList - which is used for storing the List of lending purposes. The BookList and ReaderList are type of Book and Reader respectly, I want to extract the current data from bookCode property of the Book class and readerCode property of the Reader class.
Input data
Allow a user to input lending item.
When running, the screen looks like:
Enter book code:
Enter reader code:
Enter state:
After the user enter bcode and rcode, the program check and acts as follows:
If bcode not found in the books list or rcode not found in the readers list then data is not accepted.
If both bcode and rcode found in the lending list and state=1 then data is not accepted.
If bcode and rcode found but lended = quantity then new lending item with state = 0 is added to the end of the Lending list.
If bcode and rcode found and lended < quantity then lended is increased by 1 and new lending item with state = 1 is added to the end of the Lending list.
The Books file:
package BooksPackage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
/**
*
* #author Do Van Nam
*/
class Book {
String bcode;
String btitle;
int quantity;
int lended;
double price;
Book(String code, String title, int quantity, int lended, double price) {
this.bcode = code;
this.btitle = title;
this.quantity = quantity;
this.lended = lended;
this.price = price;
}
#Override
public String toString() {
return "Book{" + "bcode=" + bcode + ", btitle=" + btitle + ", quantity=" + quantity + ", lended=" + lended + ", price=" + price + '}';
}
}
class List {
private static class Node {
Book element;
Node next;
public Node(Book e, Node next) {
this.element = e;
this.next = next;
}
public Node(Book e) {
this(e, null);
}
public Node getNext() {
return next;
}
public Book getElement() {
return element;
}
public void setNext(Node e) {
this.next = e;
}
}
Node head = null;
Node tail = null;
int size = 0;
public List() {
}
public boolean isEmpty() {
return size == 0;
}
public Book getFirst() {
if (isEmpty()) {
return null;
}
return head.element;
}
public Book getLast() {
if (isEmpty()) {
return null;
}
return tail.element;
}
public void addFirst(Book e) {
head = new Node(e, head);
if (size == 0) {
tail = head;
}
size++;
}
public void addLast(Book e) {
Node last = new Node(e, null);
if (isEmpty()) {
head = last;
} else if (size == 1) {
head.setNext(last);
tail = last;
} else {
tail.setNext(last);
tail = last;
}
size++;
}
public void removeFirst() {
if (isEmpty()) {
return;
}
head = head.getNext();
size--;
}
public void removeLast() {
if (isEmpty()) {
return;
}
Node secondLast = head;
while (secondLast.next.next != null) {
secondLast = secondLast.next;
}
secondLast.next = null;
size--;
}
public boolean isDuplicate(String code) {
Node node = head;
while (node != null) {
if (node.element.bcode.equals(code)) {
return true;
}
node = node.next;
}
return false;
}
public String displayNode() {
Node node = head;
double value;
String a = "";
while (node != null) {
value = node.element.price * node.element.quantity;
a += node.element.bcode + "\t" + node.element.btitle + "\t" + node.element.quantity + "\t" + node.element.lended + "\t" + node.element.price + "\t" + value + "\n";
node = node.getNext();
}
return a;
}
public Node searchByCode(String code) {
Node x = head;
while (x != null) {
if (x.element.bcode.equals(code)) {
return x;
}
x = x.getNext();
}
return null;
}
public void deleteByCode(String code) {
Node x = head;
if (x.element.bcode.equals(code)) {
head = head.next;
size--;
return;
}
while (x.next != null) {
if (x.next.element.bcode.equals(code)) {
x.next = x.next.next;
size--;
return;
}
x = x.next;
}
}
public void sortByBCode() {
Node a, b;
Book obj;
a = head;
while (a != null) {
b = a.next;
while (b != null) {
if (b.element.bcode.compareTo(a.element.bcode) < 0) {
obj = a.element;
a.element = b.element;
b.element = obj;
}
b = b.next;
}
a = a.next;
}
}
public void insertAfter(Node node, Book book) {
if (isEmpty() || node == null) {
return;
}
Node after = node.next;
Node newNode = new Node(book, after);
node.next = newNode;
if (tail == node) {
tail = newNode;
}
size++;
}
public Node nodeAtPos(int pos) {
int i = 0;
Node init = head;
while (init != null) {
if (i == pos) {
return init;
}
i++;
init = init.next;
}
return null;
}
public void deleteAtPostion(int pos) {
if (isEmpty()) {
return;
}
Node temp = head;
if (pos == 0) {
head = head.next;
return;
}
for (int i = 0; temp != null && i < pos - 1; i++) {
temp = temp.next;
}
if (temp == null || temp.next == null) {
return;
}
Node next = temp.next.next;
temp.next = next;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
// 1
List bookList = new List();
// 2
// a.addFirst(new Book("SA", "SOMETHING", 12, 23, 122));
// a.addFirst(new Book("SAX", "SOMETHING", 12, 23, 122));
// a.addFirst(new Book("SAC", "SOMETHING", 12, 23, 122));
// a.addFirst(new Book("SAD", "SOMETHING", 12, 23, 122));
System.out.println("1.1. Load data from file\n"
+ "1.2. Input & add to the end\n"
+ "1.3. Display data\n"
+ "1.4. Save book list to file\n"
+ "1.5. Search by bcode\n"
+ "1.6. Delete by bcode\n"
+ "1.7. Sort by bcode\n"
+ "1.8. Input & add to beginning\n"
+ "1.9. Add after position k\n"
+ "1.10. Delete position k");
int option;
do {
System.out.println("Choose an option from 1 to 10, press 0 to stop");
option = sc.nextInt();
if (option == 1) {
System.out.println("Enter the file you want to read");
String file = sc.next();
// the file will be using here is test.txt, which is existed on my local computer, you should try by entering the file you want to read on your computer instead.
BufferedReader read = new BufferedReader(new FileReader(file));
String str;
while ((str = read.readLine()) != null) {
System.out.println(str);
}
}
if (option == 2) {
System.out.println("Enter the book");
String bcode = sc.next();
String btitle = sc.next();
int quantity = sc.nextInt();
int lended = sc.nextInt();
double price = sc.nextDouble();
if (!bookList.isDuplicate(bcode)) {
bookList.addLast(new Book(bcode, btitle, quantity, lended, price));
} else {
System.out.println("This book is already in the list.");
}
}
if (option == 3) {
System.out.println("code" + "\t" + "Title" + "\t" + "Quantity" + "\t" + "Lended" + "\t" + "Price" + "\t" + "Value");
System.out.println("-------------------------------------------------------------------");
System.out.println(bookList.displayNode());
}
if (option == 4) {
System.out.println("Enter the file name");
String fileName = sc.next();
File input = new File(fileName);
if (input.createNewFile()) {
FileWriter fr = null;
BufferedWriter br = null;
String content = bookList.displayNode();
try {
fr = new FileWriter(input);
br = new BufferedWriter(fr);
// String[] lines = content.split("\r\n|\r|\n");
// int linesNums = lines.length;
br.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
br.close();
fr.close();
}
}
}
if (option == 5) {
System.out.println("Enter the code of the book you are searching");
String code = sc.next();
if (bookList.searchByCode(code) != null) {
System.out.println(bookList.searchByCode(code));
} else {
System.out.println("Not found");
}
}
if (option == 6) {
System.out.println("Enter the code of the book you want to delete");
String code = sc.next();
bookList.deleteByCode(code);
}
if (option == 7) {
bookList.sortByBCode();
}
if (option == 8) {
System.out.println("Enter the book you want to add to the beginning of the list");
System.out.println("How many books you want to add?");
int nums = sc.nextInt();
while (nums != 0) {
System.out.println("Enter bcode, title, quantity, lended and price for this book");
String bcode = sc.next();
String title = sc.next();
int quantity = sc.nextInt();
int lended = sc.nextInt();
double price = sc.nextDouble();
bookList.addFirst(new Book(bcode, title, quantity, lended, price));
nums--;
}
}
if (option == 9) {
System.out.println("Insert a new node after the bcode: ");
String code = sc.next();
System.out.println("Enter the book");
String bcode = sc.next();
String title = sc.next();
int quantity = sc.nextInt();
int lended = sc.nextInt();
double price = sc.nextDouble();
Book newBook = new Book(bcode, title, quantity, lended, price);
bookList.insertAfter(bookList.searchByCode(code), newBook);
}
if (option == 10) {
System.out.println("Enter the position you want to delete");
int pos = sc.nextInt();
bookList.deleteAtPostion(pos);
}
} while (option != 0);
}
}
The Reader file:
package BooksPackage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author VanNam
*/
class Reader {
private String rcode;
private String name;
private int byear;
Reader(String rcode, String name, int byear) {
this.rcode = rcode;
this.name = name;
this.byear = byear;
}
#Override
public String toString() {
return "Reader{" + "rcode=" + rcode + ", name=" + name + ", byear=" + byear + '}';
}
public String getRcode() {
return rcode;
}
public void setRcode(String rcode) {
this.rcode = rcode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getByear() {
return byear;
}
public void setByear(int byear) {
this.byear = byear;
}
}
class XList {
private static class Node {
Reader element;
Node next;
public Node(Reader e, Node next) {
this.element = e;
this.next = next;
}
public Node(Reader e) {
this(e, null);
}
public Node getNext() {
return next;
}
public Reader getElement() {
return element;
}
public void setNext(Node e) {
this.next = e;
}
}
public XList() {
head = tail = null;
}
Node head = null;
Node tail = null;
int size = 0;
public boolean isEmpty() {
return size == 0;
}
public Reader getFirst() {
return head.getElement();
}
public Reader getLast() {
return tail.getElement();
}
public void addFirst(Reader e) {
head = new Node(e, head);
if (size == 0) {
tail = head;
}
size++;
}
public void addLast(Reader e) {
Node last = new Node(e, null);
if (size == 0) {
head = last;
} else if (size == 1) {
head.setNext(last);
tail = last;
} else {
tail.setNext(last);
tail = last;
}
size++;
}
public String displayNode() {
Node node = head;
String a = "";
while (node != null) {
a += node.element.getRcode() + "\t" + node.element.getName() + "\t" + node.element.getByear() + "\n";
node = node.getNext();
}
return a;
}
public Node searchByCode(String code) {
Node x = head;
while (x != null) {
if (x.element.getRcode().equals(code)) {
return x;
}
x = x.getNext();
}
return null;
}
public void deleteByCode(String code) {
Node x = head;
if (x.element.getRcode().equals(code)) {
head = head.next;
size--;
return;
}
while (x.next != null) {
if (x.next.element.getRcode().equals(code)) {
x.next = x.next.next;
size--;
return;
}
x = x.next;
}
System.out.println("Not found this reader on the list");
}
public boolean isDuplicate(String code) {
Node node = head;
while (node != null) {
if (node.element.getRcode().equals(code)) {
return true;
}
node = node.getNext();
}
return false;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
XList readerList = new XList();
int option;
System.out.println("2.1. Load data from file\n"
+ "2.2. Input & add to the end\n"
+ "2.3. Display data\n"
+ "2.4. Save reader list to file\n"
+ "2.5. Search by rcode\n"
+ "2.6. Delete by rcode");
do {
Scanner sc = new Scanner(System.in);
System.out.println("Choose an option");
option = sc.nextInt();
if (option == 1) {
System.out.println("Enter the file you wanna read");
String file = sc.next();
// the file will be using here is testReader.txt, which is existed on my local computer, you should try by entering the file you want to read on your computer instead.
BufferedReader read = new BufferedReader(new FileReader(file));
String str;
while ((str = read.readLine()) != null) {
System.out.println(str);
}
}
if (option == 2) {
System.out.println("Enter the reader");
String code = sc.next();
String name = sc.next();
int year = sc.nextInt();
if (!readerList.isDuplicate(code)) {
readerList.addLast(new Reader(code, name, year));
} else {
System.out.println("This reader is already in the list.");
}
}
if (option == 3) {
System.out.println(readerList.displayNode());
}
if (option == 4) {
System.out.println("Enter the file name");
String fileName = sc.next();
File input = new File(fileName);
if (input.createNewFile()) {
FileWriter fr = null;
BufferedWriter br = null;
String content = readerList.displayNode();
try {
fr = new FileWriter(input);
br = new BufferedWriter(fr);
br.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
br.close();
fr.close();
}
}
}
if (option == 5) {
System.out.println("Find the reader by entering the code");
String code = sc.next();
if (readerList.searchByCode(code) != null) {
System.out.println("Found at this address: " + readerList.searchByCode(code));
} else {
System.out.println("Not found");
};
}
if (option == 6) {
System.out.println("Enter the reader you want to delete by entering the code");
String code = sc.next();
readerList.deleteByCode(code);
}
} while (option != 0);
}
}
The LendingBook file:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package BooksPackage;
import java.util.Scanner;
/**
*
* #author VanNam
*/
public class LendingBook {
private String bcode;
private String rcode;
private int state;
public LendingBook(String bcode, String rcode, int state) {
this.bcode = bcode;
this.rcode = rcode;
this.state = state;
}
#Override
public String toString() {
return "LendingBook{" + "bcode=" + bcode + ", rcode=" + rcode + ", state=" + state + '}';
}
public String getBcode() {
return bcode;
}
public void setBcode(String bcode) {
this.bcode = bcode;
}
public String getRcode() {
return rcode;
}
public void setRcode(String rcode) {
this.rcode = rcode;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
class LendingList {
private static class Node{
private LendingBook element;
private Node next;
public Node(LendingBook e, Node n){
this.element = e;
this.next = n;
}
public Node(LendingBook e){
this(e, null);
}
public Node getNext() {
return next;
}
public LendingBook getElement() {
return element;
}
public void setNext(Node n){
this.next = n;
}
}
Node head = null;
Node tail = null;
int size = 0;
public boolean isEmpty(){
return size == 0;
}
public LendingBook getFirst() {
if(isEmpty()) return null;
return head.getElement();
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
LendingList lendingList = new LendingList();
int option;
do{
System.out.println("Enter the option" + "\n" + "1.Input data 2. Display lending data 3. Sort by bcode + rcode" + "\n" + "Press 0 to exit");
option = sc.nextInt();
if(option == 1){
String bookCode; // -> Here, I want to check it with the existing bookCode from the Book file
String readerCode; // // -> Here, I want to check it with the existing readerCode from the Reader file
int state;
System.out.println("Enter book code");
bookCode = sc.next();
System.out.println("Enter the reader code");
readerCode = sc.next();
}
}while(option != 0);
}
}
I want to get that information and use the data from the two classes above in my LendingList. Many thanks.
You can only pass data using constructors or instance methods.
or do
filename bob = new filename(object or data type);
bob.method(object or data type);
PS : Not sure if this is going to work.
I am trying to make a generic class called reverser that takes the contents of a text file and reverses the order of the contents. I am required to do this by using the generic stack class provided. I have to make sure that it works for two types, strings, and floats as they have been instantiated in main.
I am aware I can't push int values (as is returned by BufferedInputStream) onto the stack. My question is what may be a solution to this problem, should I instantiate two different versions of the generic stack class, one for strings and one for floats, or is there another solution?
public class project_5
{
public static void main(String[] args) throws Exception
{
Reverser<String> reversePoem = new Reverser<String>();
Reverser<Float> majiGame = new Reverser<Float>();
}
}
class Reverser<E>
{
private Stack<E> tempStack;
int k = 0;
int val = 0;
public Reverser()
{
tempStack = new Stack<E>();
}
void FileToStack(String fileIn) throws Exception
{
E item = null;
try
{
BufferedInputStream readFile = new BufferedInputStream(new FileInputStream(fileIn));
for(k = 0; (val = readFile.read()) != -1; k++)
{
tempStack.push(val);
}
readFile.close();
}
catch(FileNotFoundException e)
{
System.out.print("File: " + fileIn + " Not Found");
}
catch(IOException e)
{
System.out.print("Reached end of file.");
}
}
void StackToFile(String fileIn)
{
try
{
PrintWriter fileOut = new PrintWriter(fileIn);
fileOut.println();
fileOut.close();
}
catch(FileNotFoundException e)
{
}
}
}
//Class Stack ---------------------------------------
class Stack<E>
{
// pointer to first node in stack
private Node<E> top;
// constructor
public Stack()
{
top = null;
}
public boolean isEmpty()
{
return top == null;
}
public void push(E data)
{
if (data == null)
return;
// build a node and place it on the stack
Node<E> newNode = new Node<E>(data);
newNode.next = top;
top = newNode;
}
public E pop()
{
Node<E> temp;
temp = top;
if (isEmpty())
return null;
top = top.next;
return temp.getData();
}
// console display
public String toString()
{
Node<E> p;
String showAll = "";
// Display all the nodes in the stack
for( p = top; p != null; p = p.next )
showAll += p.toString() + "\n";
return showAll;
}
}
Edit: Removed pictures, added code in text format.
I have made a change to your logic where you are reading a file and then putting into the stack,
You have not put your Node class so I have added that accordingly
You need to typecast your value when reading from file
Note: in case of the file contains improper data you will get a casting error, that you need to handle
source.txt
1 2 3 4 5
target.txt
5
4
3
2
1
Code
import java.io.*;
import java.util.Arrays;
public class Test
{
public static void main(String[] args) throws Exception
{
Reverser<String> reversePoem = new Reverser<String>(String.class);
Reverser<Float> majiGame = new Reverser<Float>(Float.class);
majiGame.FileToStack("source.txt");
majiGame.StackToFile("target.txt");
}
}
class Reverser<E>
{
private Stack<E> tempStack;
private Class<E> tClass;
public Reverser(Class<E> tClass)
{
tempStack = new Stack<E>();
this.tClass = tClass;
}
public static <E> E convertInstanceOfObject(String value, Class<E> clazz) {
try {
// add custom casting, casting could be varies as par usage
if (clazz.getName().equals("java.lang.Integer")) {
return (E)Integer.valueOf(value);
} else if (clazz.getName().equals("java.lang.Float")) {
return (E)Float.valueOf(value);
} else if (clazz.getName().equals("java.lang.String")) {
return (E)value;
}
return clazz.cast(value);
} catch(ClassCastException e) {
return null;
}
}
void FileToStack(String fileIn) throws Exception
{
BufferedReader br;
try
{
br = new BufferedReader(new FileReader(fileIn));
String line = null;
while((line = br.readLine()) != null)
{
String[] valueArray= line.split(" ");
for (String v : valueArray) {
E item = convertInstanceOfObject(v, tClass);
tempStack.push(item);
}
}
br.close();
}
catch(FileNotFoundException e)
{
System.out.print("File: " + fileIn + " Not Found");
}
catch(IOException e)
{
System.out.print("Reached end of file.");
}
}
void StackToFile(String fileIn)
{
try
{
PrintWriter fileOut = new PrintWriter(new FileWriter(fileIn));
E d = null;
while (!tempStack.isEmpty()) {
d = tempStack.pop();
fileOut.println(d);
}
fileOut.close();
}
catch(IOException e)
{
}
}
}
class Node<E> {
Node<E> next;
private E data;
public Node(E data) {
this.data = data;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
public E getData() {
return data;
}
public void setData(E data) {
this.data = data;
}
}
//Class Stack ---------------------------------------
class Stack<E>
{
// pointer to first node in stack
private Node<E> top;
// constructor
public Stack()
{
top = null;
}
public boolean isEmpty()
{
return top == null;
}
public void push(E data)
{
if (data == null)
return;
// build a node and place it on the stack
Node<E> newNode = new Node<E>(data);
newNode.next = top;
top = newNode;
}
public E pop()
{
Node<E> temp;
temp = top;
if (isEmpty())
return null;
top = top.next;
return temp.getData();
}
// console display
public String toString()
{
Node<E> p;
String showAll = "";
// Display all the nodes in the stack
for( p = top; p != null; p = p.next )
showAll += p.toString() + "\n";
return showAll;
}
}
When I run my code for a method calculating the height of a binary search tree, it results in a Stack Overflow error, but only for trees with more than one node (BSTElements in my program). I have read that this is due to a faulty recursive call, but cannot identify the problem in my code.
public int getHeight() {
return getHeight(this.getRoot());
}
private int getHeight(BSTElement<String,MorseCharacter> element) {
int height=0;
if (element == null) {
return -1;
}
int leftHeight = getHeight(element.getLeft());
int rightHeight = getHeight(element.getRight());
if (leftHeight > rightHeight) {
height = leftHeight;
} else {
height = rightHeight;
}
return height +1;
}
Here is full code:
public class MorseCodeTree {
private static BSTElement<String, MorseCharacter> rootElement;
public BSTElement<String, MorseCharacter> getRoot() {
return rootElement;
}
public static void setRoot(BSTElement<String, MorseCharacter> newRoot) {
rootElement = newRoot;
}
public MorseCodeTree(BSTElement<String,MorseCharacter> element) {
rootElement = element;
}
public MorseCodeTree() {
rootElement = new BSTElement("Root", "", new MorseCharacter('\0', null));
}
public int getHeight() {
return getHeight(this.getRoot());
}
private int getHeight(BSTElement<String,MorseCharacter> element) {
if (element == null) {
return -1;
} else {
int leftHeight = getHeight(element.getLeft());
int rightHeight = getHeight(element.getRight());
if (leftHeight < rightHeight) {
return rightHeight + 1;
} else {
return leftHeight + 1;
}
}
}
public static boolean isEmpty() {
return (rootElement == null);
}
public void clear() {
rootElement = null;
}
public static void add(BSTElement<String,MorseCharacter> newElement) {
BSTElement<String, MorseCharacter> target = rootElement;
String path = "";
String code = newElement.getKey();
for (int i=0; i<code.length(); i++) {
if (code.charAt(i)== '.') {
if (target.getLeft()!=null) {
target=target.getLeft();
} else {
target.setLeft(newElement);
target=target.getLeft();
}
} else {
if (target.getRight()!=null) {
target=target.getRight();
} else {
target.setRight(newElement);
target=target.getRight();
}
}
}
MorseCharacter newMorseChar = newElement.getValue();
newElement.setLabel(Character.toString(newMorseChar.getLetter()));
newElement.setKey(Character.toString(newMorseChar.getLetter()));
newElement.setValue(newMorseChar);
}
public static void main(String[] args) {
MorseCodeTree tree = new MorseCodeTree();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) {
String[] output = line.split(" ");
String letter = output[0];
MorseCharacter morseCharacter = new MorseCharacter(letter.charAt(0), output[1]);
BSTElement<String, MorseCharacter> bstElement = new BSTElement(letter, output[1], morseCharacter);
tree.add(bstElement);
line = reader.readLine();
System.out.println(tree.getHeight());
}
reader.close();
} catch (IOException e) {
System.out.println("Exception" + e);
}
There doesn't appear to be anything significantly wrong1 with the code that you have shown us.
If this code is giving a StackOverflowException for a small tree, that most likely means that your tree has been created incorrectly and has a cycle (loop) in it. If your recursive algorithm encounters a cycle in the "tree" it will loop until the stack overflows2.
To be sure of this diagnosis, we need to see an MVCE which includes all code needed to construct an example tree that exhibits thie behavior.
1 - There is possibly an "off by one" error in the height calculation, but that won't cause a stack overflow.
2 - Current Java implementations do not do tail-call optimization.
I tried running this code that is a move-to-front encoder, but I get this exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MTFencoder.main(MTFencoder.java:10)
Could someone explain why this happens?
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;
import java.io.IOException;
public class MTFencoder {
public static void main(String[] args) {
String filename = args[0];
WordList wordList = new WordList();
try {
String line;
BufferedReader br = new BufferedReader(new FileReader(filename));
StringTokenizer st;
int index;
while ((line = br.readLine()) != null) {
st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
String currentword = st.nextToken();
if (wordList.hasElement(currentword)) {
index = wordList.Index(currentword);
wordList.remove(currentword);
wordList.add(currentword);
System.out.println(Integer.toString(index));
} else {
wordList.add(currentword);
System.out.println("0" + currentword);
}
}
}
} catch (IOException e) {
}
}
}
class Node {
private String word;
private Node next;
public Node(String w) {
word = w;
next = null;
}
public String getWord() {
return word;
}
public Node getNext() {
return next;
}
public void setNext(Node n) {
next = n;
}
}
class WordList {
Node head;
public WordList() {
}
public void add(String w) {
Node n = new Node(w);
n.setNext(head);
head = n;
}
public int length() {
Node tmp = head;
int count = 0;
while (tmp != null) {
count++;
tmp = tmp.getNext();
}
return count;
}
public boolean hasElement(String w) {
Node tmp = head;
while (tmp != null) {
if (tmp.getWord().equals(w)) return true;
tmp = tmp.getNext();
}
return false;
}
public void remove(String w) {
if (!hasElement(w)) return;
if (head.getWord().equals(w)) {
head = head.getNext();
return;
}
Node tmp = head;
while (!(tmp.getNext().getWord().equals(w))) tmp = tmp.getNext();
tmp.setNext(tmp.getNext().getNext());
}
public void dumpList() {
for (Node tmp = head; tmp != null; tmp = tmp.getNext())
System.out.println(" >> " + tmp.getWord());
}
public int Index(String w) {
int counter = 1;
Node temp = head;
while (!(temp.getWord().equals(w))) {
counter++;
temp = temp.getNext();
}
return counter++;
}
}
The program expects command line arguments. You aren't passing it.
The program needs to be run as java MTFencoder fileyouwanttoprocess. You aren't passing the filename parameter.
This is line 10
String filename = args[0];
args[0] is the first command line argument - you aren't passing command line arguments.