when I tried running this, an error occured on this line: Node2 newNode = new No
ID: 3634840 • Letter: W
Question
when I tried running this, an error occured on this line: Node2 newNode = new Node2();... the error that came out is that the symbol cannot be found.. and the arrow pointed to the word "new"1st file:
import java.util.Scanner;
public class Binary2
{
public static void main (String [] args)
{
Scanner scan = new Scanner (System.in);
Node2 newNode = new Node2();
while (scan.hasNextInt())
{
insert (newNode, scan.nextInt());
}
}
}
2nd file:
public class Node2
{
Node left;
Node right;
int data;
public Node2 (int data)
{
this.data = data;
}
public void insert (Node node, int data)
{
if (data <= node.data)
{
if (node.left != null)
{
insert (node.left, data);
}
else
{
System.out.println(" Inserted: " + data + " to the left of node: " + node.data);
node.left = new Node (data);
}
}
else if (data > node.data)
{
if (node.right != null)
{
System.out.println(" Inserted: " + data + " to the right of node: " + node.data);
insert (node.right, data);
}
else
{
node.right = new Node (data);
}
}
}
}
Explanation / Answer
new should be given a argument... i.e. Node2 newNode = new Node2(data);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.