Results 1 to 2 of 2
  1. #1
    gmoney8316 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default Help with objects

    So I have written a program that is below in which I am trying to create an object (Person) that can host all of the information for a single person and then create an array with that data type. The problem is this program compiles but that it doesn't run correctly. Can anyone tell me where I am screwing this up? I have placed the second part of this at the very bottom.

    import java.io.*;
    import java.util.Scanner;
    import javax.swing.JOptionPane;

    public class Lab11
    {
    public static String lastName[] = new String [50];
    public static String firstName[] = new String [50];
    public static String eMail[] = new String [50];
    public static String homePhone[] = new String [50];
    public static String cellPhone[] = new String [50];
    public static String city[] = new String [50];
    public static String state[] = new String [50];

    public static void main(String[] args)
    {
    int choice = 0;

    while (choice >= 0)
    {
    try
    {
    choice = Integer.parseInt (JOptionPane.showInputDialog("What would you like to do?\n1) Open a file \n2) Display the information in the address book \n3) Search for information by last name \n4) Find information by last name and replace the phone number or first name \n5) Save the address book \n6) End program"));
    }

    catch(Exception e)
    {
    }
    switch (choice)
    {
    case 1: loadFile();
    break;
    case 2: display();
    break;
    case 3: search();
    break;
    case 4: changeName();
    break;
    case 5:
    try
    {
    save();
    }
    catch(Exception e)
    {System.out.println ("Error Found");}

    break;
    case 6: choice = -1;
    break;
    default: System.out.println("Enter a number");

    choice = 0;
    }
    }
    System.out.println("Adios");
    }


    public static void loadFile()
    {
    int c = 0;

    Person[] person = new Person[50];
    for(int b = 0; b < person.length; b++)
    person[b] = new Person();


    try
    {
    Scanner scanner = new Scanner(new File("addressBook.csv"));
    scanner.hasNextLine();
    scanner.nextLine();

    while(scanner.hasNextLine())
    {
    String temp = scanner.nextLine();
    Scanner lineScanner = new Scanner(temp);
    lineScanner.useDelimiter(",");

    while(lineScanner.hasNext())
    {
    person[c].lastName = lineScanner.next();
    person[c].firstName = lineScanner.next();
    person[c].eMail =lineScanner.next();
    person[c].homePhone = lineScanner.next();
    person[c].cellPhone = lineScanner.next();
    person[c].city = lineScanner.next();
    person[c].state = lineScanner.next();

    c++;
    }
    }
    }
    catch (Exception e)
    {
    System.out.println("a problem occured");
    }


    System.out.println("Loaded");
    }


    public static void display()
    {
    Person[] person = new Person[50];
    for(int b = 0; b < person.length; b++)
    person[b] = new Person();

    for (int c = 0; c < 7; c++)
    {
    System.out.println(person[c].firstName + " " + person[c].lastName + ": " + eMail[c]);
    System.out.println("Home number: " + person[c].homePhone);
    System.out.println("Cell number: " + person[c].cellPhone);
    System.out.println("Lives in: " + person[c].city + "," + person[c].state);
    }
    }

    public static void search()
    {
    Person[] person = new Person[50];
    for(int b = 0; b < person.length; b++)
    person[b] = new Person();

    String search = "";

    search = JOptionPane.showInputDialog("What last name to search for?");

    for (int c = 0; c < lastName.length; c++)
    {
    if (search.equals(person[c].lastName))
    System.out.println("Record is found!");
    }
    }

    public static void changeName()
    {
    Person[] person = new Person[50];
    for(int b = 0; b < person.length; b++)
    person[b] = new Person();

    int choice;
    String search = "";
    String newSearch = "";
    search = JOptionPane.showInputDialog("What last name would you like to change?");


    for (int c = 0; c < lastName.length; c++)
    {
    if (search.equals(person[c].lastName))
    {
    System.out.println("Found");

    choice = Integer.parseInt (JOptionPane.showInputDialog("Which one would you like to change?\n1(Phone Number\n2(First Name"));

    switch (choice)
    {
    case 1:
    person[c].cellPhone = JOptionPane.showInputDialog("What is the New Phone Number?");
    break;

    case 2:
    person[c].firstName = JOptionPane.showInputDialog("What is the New First Name?");
    break;
    default: System.out.println("Enter 1 or 2");
    }
    }
    }
    }
    public static void save()
    throws FileNotFoundException
    {
    Person[] person = new Person[50];
    for(int b = 0; b < person.length; b++)
    person[b] = new Person();

    PrintStream addressBook = new PrintStream(new File("addressBook2.csv"));

    addressBook.println("last name, first name, email address, home phone, cell phone, city, state");
    for(int c = 0; c < 7; c++)
    {
    addressBook.print(person[c].lastName + "," + person[c].firstName + "," + person[c].eMail);
    addressBook.print("," + person[c].homePhone);
    addressBook.print("," + person[c].cellPhone);
    addressBook.println("," + person[c].city + "," + person[c].state);
    }
    addressBook.close();

    System.out.println("Saved");
    }
    }




    public class Person
    {
    public String lastName;
    public String firstName;
    public String eMail;
    public String homePhone;
    public String cellPhone;
    public String city;
    public String state;
    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Please use CODE tags and format your code to make it readable for us. I don't want to be mean or discouraging, but there are simply so many things wrong here that it is difficult to know where to start.

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    public class Lab11
    {
        public static String lastName[] = new String [50];
        public static String firstName[] = new String [50];
        public static String eMail[] = new String [50];
        public static String homePhone[] = new String [50];
        public static String cellPhone[] = new String [50];
        public static String city[] = new String [50];
        public static String state[] = new String [50];
    I don't think you want these instance variables at all, from what you said in your post. I think you want one array of Person objects. You want it to be private, not public. You don't want it to be static. And while
    Java Code:
    [COLOR="Red"]    private Person persons[] = new Person[50];
    [/COLOR]
    is legal, it is not good style. This is better:
    Java Code:
    [COLOR="Blue"]    private Person[] persons = new Person[50];
    [/COLOR]
    And in a case like this (where you'll be reading the data in from a file) an ArrayList is probably better than a raw array, but you may not have learned about ArrayLists yet, so I'll leave that alone.
    Java Code:
        public static void main(String[] args)
        {
            int choice = 0;
    
            while (choice >= 0)
            {
                try
                {
                    choice = Integer.parseInt (JOptionPane.showInputDialog(
                        "What would you like to do?\n" + 
                         "1) Open a file \n" + 
                         "2) Display the information in the address book \n" +
                         "3) Search for information by last name \n" + 
                         "4) Find information by last name and replace the phone number or first name \n" +
                         "5) Save the address book \n" + 
                         "6) End program"));
                }
                catch(Exception e)
                {
    Really? You're going to catch the Exception and totally ignore it? Do you really think that's a good idea?
    Java Code:
                }
                switch (choice)
                {
                    case 1: loadFile();
                        break;
                    case 2: display();
                        break;
                    case 3: search();
                        break;
                    case 4: changeName();
                        break;
                    case 5:
                        try
                        {
                            save();
                        }
                        catch(Exception e)
                        {System.out.println ("Error Found");}
                        break;
                    case 6: choice = -1;
                        break;
                    default: System.out.println("Enter a number");
    
                        choice = 0;
                }
            }
            System.out.println("Adios");
        }
    
        public static void loadFile()
        {
            int c = 0;
    
            Person[] person = new Person[50];
            for(int b = 0; b < person.length; b++)
                person[b] = new Person();
    Do you know you have info for 50 Persons in the file? Of course it doesn't really matter much, because person is a local variable declared in this method and thrown away at the end of it.
    Java Code:
            try
            {
                Scanner scanner = new Scanner(new File("addressBook.csv"));
                scanner.hasNextLine();
                scanner.nextLine();
    
                while(scanner.hasNextLine())
                {
                    String temp = scanner.nextLine();
                    Scanner lineScanner = new Scanner(temp);
                    lineScanner.useDelimiter(",");
    
                    while(lineScanner.hasNext())
    This line is rather meaningless, because you are only checking the first field on each line, and if you have a malformed file, then some of these next() calls will fail, and all you'll get is a vague "a problem occured".
    Java Code:
                    {
                        person[c].lastName = lineScanner.next();
                        person[c].firstName = lineScanner.next();
                        person[c].eMail =lineScanner.next();
                        person[c].homePhone = lineScanner.next();
                        person[c].cellPhone = lineScanner.next();
                        person[c].city = lineScanner.next();
                        person[c].state = lineScanner.next();
    
                        c++;
                    }
                }
            }
            catch (Exception e)
            {
                System.out.println("a problem occured");
    But no hint about what the problem was, and we're going to keep on running anyway?
    Java Code:
            }
    
            System.out.println("Loaded");
        }
    
    
        public static void display()
        {
            Person[] person = new Person[50];
            for(int b = 0; b < person.length; b++)
                person[b] = new Person();
    Now that we've declared and initialized an array of 50 new empty Person objects, we'll print out some empty information on the first seven of them.
    Java Code:
            for (int c = 0; c < 7; c++)
            {
                System.out.println(person[c].firstName + " " + person[c].lastName + ": " + eMail[c]);
                System.out.println("Home number: " + person[c].homePhone);
                System.out.println("Cell number: " + person[c].cellPhone);
                System.out.println("Lives in: " + person[c].city + "," + person[c].state);
            }
        }
    
        public static void search()
        {
            Person[] person = new Person[50];
            for(int b = 0; b < person.length; b++)
                person[b] = new Person();
    
            String search = "";
    
            search = JOptionPane.showInputDialog("What last name to search for?");
    
            for (int c = 0; c < lastName.length; c++)
    What is lastName here?
    Java Code:
            {
                if (search.equals(person[c].lastName))
                    System.out.println("Record is found!");
    You just want to say it's found and not do anything with it?
    Java Code:
            }
        }
    
        public static void changeName()
        {
            Person[] person = new Person[50];
            for(int b = 0; b < person.length; b++)
                person[b] = new Person();
    
            int choice;
            String search = "";
            String newSearch = "";
            search = JOptionPane.showInputDialog("What last name would you like to change?");
    
            for (int c = 0; c < lastName.length; c++)
    Again, what is lastName here?
    Java Code:
            {
                if (search.equals(person[c].lastName))
                {
                    System.out.println("Found");
    
                    choice = Integer.parseInt (JOptionPane.showInputDialog(
                        "Which one would you like to change?\n" + 
                        "1(Phone Number\n" + 
                        "2(First Name"));
    
                    switch (choice)
                    {
                        case 1:
                            person[c].cellPhone = JOptionPane.showInputDialog("What is the New Phone Number?");
                            break;
    
                        case 2:
                            person[c].firstName = JOptionPane.showInputDialog("What is the New First Name?");
                            break;
                        default: System.out.println("Enter 1 or 2");
                    }
                }
            }
        }
    
        public static void save() throws FileNotFoundException
       {
            Person[] person = new Person[50];
            for(int b = 0; b < person.length; b++)
                person[b] = new Person();
    
            PrintStream addressBook = new PrintStream(new File("addressBook2.csv"));
    
            addressBook.println("last name, first name, email address, home phone, cell phone, city, state");
            for(int c = 0; c < 7; c++)
    Now we will try to write to our output file the contents of the first seven freshly initialized, empty Person objects.
    Java Code:
            {
                addressBook.print(person[c].lastName + "," + person[c].firstName + "," + person[c].eMail);
                addressBook.print("," + person[c].homePhone);
                addressBook.print("," + person[c].cellPhone);
                addressBook.println("," + person[c].city + "," + person[c].state);
            }
            addressBook.close();
    
            System.out.println("Saved");
        }
    }
    This goes in a separate Person.java file of course:
    Java Code:
    public class Person
    {
        public String lastName;
        public String firstName;
        public String eMail;
        public String homePhone;
        public String cellPhone;
        public String city;
        public String state;
    }
    This is better, but make them private, write a constructor, and write getter and setter methods. Then use them in the above code.

    OK, it's not really as bad as I thought at first -- I've certainly seen much worse. It's basically the same couple of errors repeated throughout, but much of your other code is just fine. Replace those instance variables with a single Person[] array (or even better, an ArrayList<Person>), and use that instance variable in all your other methods. Revisit your Exception handling text, and think about what you really should be doing.

    -Gary-
    Last edited by gcalvin; 05-06-2010 at 03:09 AM.

Similar Threads

  1. How to map 2 different objects together?
    By einnhann in forum New To Java
    Replies: 5
    Last Post: 12-22-2009, 10:42 AM
  2. Replies: 1
    Last Post: 01-22-2009, 05:25 PM
  3. Two Objects
    By losintikfos in forum New To Java
    Replies: 3
    Last Post: 11-14-2008, 08:04 PM
  4. 1 to 1 Objects
    By this.that in forum New To Java
    Replies: 4
    Last Post: 08-07-2008, 11:09 PM
  5. how many objects ?
    By kevinsong in forum Advanced Java
    Replies: 16
    Last Post: 07-16-2008, 06:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •