hey guys.. basically i'm writing a program that will let the user enter an int and the program will insert that that value in to a double-link-list(dll) along with calculating the sqr root value of it in that same node and it's inserted to the node by increasing int value then displaying the data.
I got most of it except the insert method that I made is acting odd and I can't figure out why it's doing that. Basically it will let me insert the first int to the dll, but if I enter a value that's GREATER than it there is an error.
Please enter an int: 3
[ 3 ] 1.73
Please enter an int: 8
Exception in thread "main" java.lang.NullPointerException
at Project3.Dll.insert(Dll.java:56)
at Project3.dllDrive.main(dllDrive.java:8)
However, when I enter a second value that's less than my first, the program runs. Except it keeps the first value at the end of the dll and won't sort.
Please enter an int: 5
[ 5 ] 2.24
Please enter an int: 2
[ 2 ] 1.41 [ 5 ] 2.24
Please enter an int: 9
[ 2 ] 1.41 [ 9 ] 3.00 [ 5 ] 2.24
Please enter an int: 4
[ 2 ] 1.41 [ 9 ] 3.00 [ 4 ] 2.00 [ 5 ] 2.24
Please enter an int: 1
[ 1 ] 1.00 [ 2 ] 1.41 [ 9 ] 3.00 [ 4 ] 2.00 [ 5 ] 2.24
Here is my code.. any help/insight as to why my program is doing this is greatly appreciated! :)
class Dll{
private Node front;
public Dll(){
front = null;
}
int length = 0;
public boolean isEmpty(){
return front == null;
}
public void print(){
DecimalFormat twoDec = new DecimalFormat("###.00");
Node p = front;
while(p != null){
System.out.print(" [ " + p.getNumber() + " ] " + twoDec.format(p.getRoot()));
p = p.getRight();
}
System.out.println("");
}
public int read(){
Scanner scan = new Scanner(System.in);
int num = 0;
System.out.print("Please enter an int: ");
num = scan.nextInt();
return num;
}
/*
Creates a node that contains n and its sqr root, inserts it in to the dll
*/
public void insert(int n){
Node temp = new Node(n);
// insert in front if empty
if (isEmpty()){
front = temp;
}
else {
Node p = null;
Node q = front;
// insert in order of int
if (temp.getNumber() < q.getNumber()){
temp.setRight(q);
q.setLeft(temp);
front = temp;
} else if (temp.getNumber() > q.getNumber()) {
while (q != null && q.getRight() != null){
p = q;
q = q.getRight();
}
p.setRight(temp);
temp.setLeft(p);
temp.setRight(q);
q.setLeft(temp);
}
}
}
}
import java.lang.*;
class Node {
private Node left;
private int number;
private double root;
private Node right;
public Node() {
left = null;
number = 0;
root = 0.0;
right = null;
}
public Node(int x) {
left = null;
number = x;
root = Math.sqrt(x);
right = null;
}
// primary constructor
public Node(int x, Node l, Node r) {
left = l;
number = x;
root = Math.sqrt(x);
right = r;
}
// accessor method
public int getNumber() {
return number;
}
public double getRoot(){
return root;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
// mutator method
public void setNum(int x){
number = x;
}
public void setLeft(Node leftNode) {
left = leftNode;
}
public void setRight(Node rightNode) {
right = rightNode;
}
} // end of class Node
public class dllDrive {
public static void main(String [] args){
Dll numbers = new Dll();
int num;
int i = 0;
for (i = 0; i < 5; i++){
num = numbers.read();
numbers.insert(num);
numbers.print();
}
}
}