Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Simple Address Book Program Not Working....

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Simple Address Book Program Not Working....

    Hello everyone. I am just getting into java and I saw on other forums on the web that an address book would be a good program to code for a beginner - intermediate level coder. I know C++ quite well, and Java is still a little foreign to me.

    Could you guys look at my code and try and fix it and/or tell me what is wrong with it. Thank you so much!

    package address.book.program;


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class AddressBookProgram {
    static Contact contactArray[] = new Contact[100];
    public static void main(String[] args) {
    DisplayCurrentObject(0);
    }
    public static void DisplayCurrentObject(int index){
    String firstName = Contact.getFirstName();
    String lastName = Contact.getLastName();
    String address1 = Contact.getAddress1();
    String address2 = Contact.getAddress2();
    String city = Contact.getCity();
    String province = Contact.getProvince();
    String postal= Contact.getPostal();

    System.out.println("|-----------------------------------------------|");
    System.out.println("|Contact #: " + index + " |");
    System.out.println("|-----------------------------------------------|");
    System.out.println("|First Name: " + firstName + " |");
    System.out.println("|Last Name: " + lastName + " |");
    System.out.println("|Address1: " + address1 + " |");
    System.out.println("|Address2: " + address2 + " |");
    System.out.println("|City: " + city + " |");
    System.out.println("|Province: " + province + " |");
    System.out.println("|Postal Code: " + postal + " |");
    System.out.println("|-----------------------------------------------|");
    System.out.println("| 'P': Previous 'N': Next |");
    System.out.println("|'U': Update 'D': Delete 'C': Create |");
    System.out.println("|-----------------------------------------------|");
    InputCrudCommand(index);
    }


    public static void ProgramPathGate(char CrudCommandInputValue, int index){
    if (CrudCommandInputValue == 'p') { // Previous
    if (index == 0) {
    DisplayCurrentObject(0);
    }
    else {
    index--;
    DisplayCurrentObject(index);
    }
    }
    if (CrudCommandInputValue == 'n') { // Next
    if (index == 100) {
    DisplayCurrentObject(0);
    }
    else {
    index++;
    DisplayCurrentObject(index);
    }
    }
    if (CrudCommandInputValue == 'u') { // Update
    UpdateContact(index);
    }
    if (CrudCommandInputValue == 'd') { // Delete
    DeleteContact(index);
    }
    if (CrudCommandInputValue == 'c') { // Create
    CreateContact();
    }
    }

    public static void CreateContact() {

    String currentInfo;
    int index = 0;

    if((currentInfo = NewContactInput()) != null){
    Contact contact = new Contact();
    String newlyInputedContactInfo[] = currentInfo.split(",");
    Contact.setFirstName(newlyInputedContactInfo[0]); // insert this stuff into update class method
    Contact.setLastName(newlyInputedContactInfo[1]);
    Contact.setAddress1(newlyInputedContactInfo[2]);
    Contact.setAddress2(newlyInputedContactInfo[3]);
    Contact.setCity(newlyInputedContactInfo[4]);
    Contact.setProvince(newlyInputedContactInfo[5]);
    Contact.setPostal(newlyInputedContactInfo[6]);

    contactArray[index] = contact;
    index++;
    DisplayCurrentObject(index);
    }
    if(NewContactInput() == null) {
    System.out.println("Maximum Number of Contacts Reached.");
    DisplayCurrentObject(0);
    }
    }

    public static void DeleteContact(int index){

    }

    public static void UpdateContact(int index){

    }

    public static void InputCrudCommand(int index) {
    String CrudCommandInput = null;
    char CrudCommandInputValue = 0;
    try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(" ");
    System.out.print(" Choice: ");
    CrudCommandInput = reader.readLine();
    CrudCommandInputValue = CrudCommandInput.charAt(0);
    reader.close();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (NumberFormatException e) {
    System.out.println("Input is invalid.");
    }
    ProgramPathGate(CrudCommandInputValue, index);
    }

    public static String NewContactInput(){
    String userInput = null;
    int i = 0;
    try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter your: First Name, Last Name, Address1, Address2, City, Province, and Postal Code all separated by a comma (,): ");
    userInput = reader.readLine();
    reader.close();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (NumberFormatException e) {
    System.out.println("Input is invalid.");
    }
    i++;
    if (i == 100) {return null;}
    return userInput;
    }

    }
    class Contact {

    static String firstName, lastName, address1, address2, city, province, postal;

    public static void setFirstName (String str) {
    firstName = str;

    }
    public static String getFirstName () {
    return firstName;
    }

    public static void setLastName (String str) {
    lastName = str;
    }
    public static String getLastName () {
    return lastName;
    }

    public static void setAddress1 (String str) {
    address1 = str;
    }
    public static String getAddress1 () {
    return address1;
    }

    public static void setAddress2 (String str) {
    address2 = str;
    }
    public static String getAddress2 () {
    return address2;
    }

    public static void setCity (String str) {
    city = str;
    }
    public static String getCity () {
    return city;
    }

    public static void setProvince (String str) {
    province = str;
    }
    public static String getProvince () {
    return province;
    }

    public static void setPostal (String str) {
    postal = str;
    }
    public static String getPostal () {
    return postal;
    }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Simple Address Book Program Not Working....

    Quote Originally Posted by programmer101 View Post
    Could you guys look at my code and try and fix it and/or tell me what is wrong with it. Thank you so much!
    While that would suit you, that would take far too much time to do for every question. You tell us what is wrong with it. Does it compile or have compile errors? Does it run or have runtime errors? Does it run but produce incorrect results or output?
    Also please see the announcements page for the use of code tags on the forum.

Similar Threads

  1. Address Book program in Java
    By DaveNAchill in forum Paid Java Projects
    Replies: 3
    Last Post: January 6th, 2012, 04:03 AM
  2. Address Book Program in java
    By DaveNAchill in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 09:25 AM
  3. Address Book Program Issues
    By Gamb1t in forum What's Wrong With My Code?
    Replies: 208
    Last Post: August 25th, 2011, 08:43 PM
  4. Address Book Help
    By clevel211 in forum Object Oriented Programming
    Replies: 4
    Last Post: November 21st, 2010, 09:42 PM
  5. Array of contacts (address book)
    By smush in forum Collections and Generics
    Replies: 11
    Last Post: April 29th, 2010, 03:08 AM