In the code below. I a trying to allow the user to enter entries into the program. Right now they can only add one entry into the program. How can I edit it so the user can have the option to enter another entry into the program.
Example of output:
Enter ID: 444
Enter FName: John
Enter Lname: Thompson
Would you like to enter another entry Y/N?Y
Enter ID:1
Enter FName: Gail
Enter Lname: Jennings
Would you like to enter another entry Y/N?N
If yes is clicked I want to add the gathered data into the binary tree and allow the user to enter another id. How can I do that?
import java.util.Scanner;
class clubmember {
public static void main(String[] args) {
int id;
String fname, lname;
Scanner input = new Scanner(System.in);
System.out.println("Enter ID>");
id = input.nextInt();
System.out.println("Enter first name >");
fname = input.next();
System.out.println("Enter last name >");
lname = input.next();
BinaryTreeTest foo = new BinaryTreeTest();
Person per1 = new Person(id, fname, lname);
BinaryTreeTest.Node nod1 = new BinaryTreeTest.Node(per1);
Person per2 = new Person(734, "Smith", "Rick");
Person per3 = new Person(324, "Gates", "Jill");
foo.insert(nod1, per2);
foo.insert(nod1, per3);
foo.printInOrder(nod1);
}
}
public class BinaryTreeTest {
public static void main(String[] args) {
new BinaryTreeTest().run();
}
// Node Class
static class Node {
Node left;
Node right;
Person value;
public Node(Person value) {
this.value = value;
}
}
public void run() {
}
public void insert(Node node, Person value) {
if (value.getId() < node.value.getId()) {
if (node.left != null) {
insert(node.left, value);
} else {
System.out.println(" Inserted " + value + " to left of "
+ node.value);
node.left = new Node(value);
}
} else if (value.getId() > node.value.getId()) {
if (node.right != null) {
insert(node.right, value);
} else {
System.out.println(" Inserted " + value + " to right of "
+ node.value);
node.right = new Node(value);
}
}
}
public void printInOrder(Node node) {
if (node != null) {
printInOrder(node.left);
System.out.println(" Traversed " + node.value);
printInOrder(node.right);
}
}
}
public class Person {
private final int id;
private final String firstName;
private final String lastName;
public Person(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return String.valueOf(id) + ": " + firstName + " " + lastName;
}
}
Consider using a
do { ... } while ()loop. Something like