-3

at the moment, I'm exercising with linked list but I have a problem with a code. The code below runs and work, but when I'm trying to add some nodes using a for generating random numbers it gave me this error. Before adding the for the code works and run adding the now as you can see into main. Maybe I missed something. Can someone help me to understand?

P.S. The commented part was the part of the main that I tryed to "upgrade".

import java.util.Random;

class Node {
    private int value;
    private Node next = null;

    public Node(int value) {
        this.value = value;
    }

    public int getValue() { return this.value; }

    public Node getNext() { return this.next; }

    public void setNext(Node pNext) { this.next = pNext; }

}

public class linked {

    private Node head;
    private Node tail;
    private int size;

    public int getSize() { return this.size; }

    public void insert (Node ele) {
        if (this.head == null) {
            this.tail = ele;
            this.head = this.tail;
        }
        else {
                this.tail.setNext(ele);
                this.tail = ele;
        }
        this.size++;
    }

    @Override
    public String toString() {
        StringBuilder ret = null;
        if ((this.head != null) && (this.tail != null)) {

            ret = new StringBuilder("[Dimensione: " + this.size
                                                  + ", Head: "
                                                  + this.head.getValue()
                                                  + ", Tail: "
                                                  + this.tail.getValue()
                                                  + "] Elementi: ");
            Node tmp = this.head;
            while (tmp != null) {
                ret.append(tmp.getValue() + " -> ");
                tmp = tmp.getNext();
            }
            ret.append("/");
        }
        return ret == null ? "[null]" : ret.toString();
    }

    public static void main (String args[])
    {
        linked ll = new linked();
        System.out.println(ll);

        for(int i=0; i<15; i++) {
            Random rand = new Random();
            double pazz = rand.nextInt(50) + 1;
            ll.insert(new Node(pazz));
        }
        /*
        ll.insert(new Node(10));
        System.out.println(ll);

        ll.insert(new Node(25));
        System.out.println(ll);

        ll.insert(new Node(12));
        System.out.println(ll);

        ll.insert(new Node(20));
        System.out.println(ll);
        */
    }
}
PoByBolek
  • 3,775
  • 3
  • 21
  • 22
Giovanni Giampaolo
  • 465
  • 1
  • 9
  • 17
  • 3
    1 - declare `pazz` as int. 2 - Move `new Random` outside the loop – zlakad Jan 02 '19 at 19:21
  • 1
    Why did you declare `pazz` as a `double`, when you assign it an `int` value, and want to pass it to a method expecting `int`? Change declaration to `int`. – Andreas Jan 02 '19 at 19:22

1 Answers1

1
double pazz = rand.nextInt(50) + 1;

Here you set pazz as double. You should set it to int instead.