-1

I'm making an address book program and I'm to add/delete/find people.

This is my contact class

import java.util.Scanner;
public class Contacts {

String name;
String lastn;
String phone;

public Contacts () {
    Scanner sc = new Scanner (System.in);
    System.out.println ("Enter the first name >");
    String n = sc.next();
    System.out.println ("Enter the last name >");
    String l = sc.next();
    System.out.println ("Enter the phone number (use the format xxx-xxx-xxxx) >");
    String p = sc.next();
    name = n + " " + l; phone = p;
}

public String getName () { return name; }

public String getPhone () {return phone;}

public String toString () {
    String result = name + "\n" + phone;
    return result;
}
}

and this is my main class

import java.util.Scanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInput;
import java.io.PrintWriter;

public class Main {
static final String filePath = System.getProperty("user.dir") + "\\src\\files";
static final String fileName = "ContactInfo.dat";

public static void main(String[] args) {



    Scanner sc = new Scanner (System.in);

    BST<String,Contacts> tree1 = new BST <String,Contacts>();

    Contacts a1 = new Contacts ();

    System.out.println (tree1);

    tree1.insert(a1.getName(), a1);

    System.out.println (tree1);

    System.out.println(a1.getName());

}
}

So heres my thing, is it possible for me to use a scanner for everything that I do? For example. When I want to add someone in I have a method for that but, its resolved to a single variable a1, how would I automatically have that be resolved to a2 for the next instance?

Furthermore how would I go about deleting someone from the book using the scanner? I can do it all in the code but obviously thats not ideal for an address book?

Uri D. Charles
  • 57
  • 1
  • 1
  • 9

1 Answers1

0

I think your question is not clear but let me answer you as I understand.

First, think scanner object just for getting input from user. Adding/deleting people is irrelevant with scanner. If you want to have an address book forget about a1 you can use it temporary because you are holding contact in BST, don't have to have a2, a3 ...

Here is a simple code for your main class.

import java.util.Scanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInput;
import java.io.PrintWriter;

public class Main {
static final String filePath = System.getProperty("user.dir") + "\\src\\files";
static final String fileName = "ContactInfo.dat";

public static void main(String[] args) {



    Scanner sc = new Scanner (System.in);

    BST<String,Contacts> tree1 = new BST <String,Contacts>();

    for(int i = 1; i <= 5; i++) {
        Contacts a1 = new Contacts ();

        System.out.println (tree1);

        tree1.insert(a1.getName(), a1);

        System.out.println (tree1);

        System.out.println(a1.getName());
    }

}
halit
  • 177
  • 7