Solved

Methods won't compile

Posted on 2008-10-22
7
166 Views
Last Modified: 2013-11-23
This code below is supposed to create an address book. It uses two classes but two methods won't work. What am I possibly doing wrong? I've been working on this for hours and still no such luck!

Is it the scanner that I'm doing wrong, if so, what has to happen? Totally confused!


   Thanks for your help!
/** HERE IS THE FIRST CLASS **/
 
public class Person
{
    private String firstName;
    private int yearBorn;
    private String phone;
    
    public Person()
    {
        firstName = "";
        yearBorn = 0;
        phone = "";
    }
 
    public Person(String firstNameX, int yearBornX, String phoneX)
    {
        firstName = firstNameX;
        yearBorn = yearBornX;
        phone = phoneX;
    }
    
    public void setFirstName(String firstNameX)
    {
        firstName = firstNameX;
    }
   
    public String getFirstName()
    {
        return (firstName);
    }
 
    public void setYearBorn(int yearBornX)
    {
        yearBorn = yearBornX;
    }
 
    public int getYearBorn()
    {
        return (yearBorn);
    }
 
    public void setPhone(String phoneX)
    {
        phone = phoneX;
    }
 
    public String getPhone()
    {
        return (phone);
    }
 
    public void print()
    {
       System.out.println("name=" + firstName + "\t" + "yearBorn=" + yearBorn + "\t" + "phone=" + phone);
    }
}
 
/**HERE IS THE SECOND CLASS**/
 
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class AddressBook
{
    Person[] people = new Person[10];
 
 
 
 
    public void addPerson(Person newPerson)
    {
        for(int indexPeople = 0; indexPeople<people.length; indexPeople++)
        {
            if(people[indexPeople] == null)
            {
                people[indexPeople] = newPerson;
            }
        }
    }
 
    public void removePerson(String firstName)
    {
        for(int indexPeople = 0; indexPeople<people.length; indexPeople++)
        {
            String[] newPeople = new String[people.length];
            if(newPeople[indexPeople] == firstName)
            {
                people[indexPeople] = null;
            }
        }
    }
 
    public void print()
    {
        for(int indexPeople = 0; indexPeople<people.length; indexPeople++)
        {
            System.out.println(people[indexPeople]);
        }
    }
    
   
    
 
    public static void main(String[] args)
    {
        AddressBook book = new AddressBook();
        
        Scanner scanner = new Scanner(System.in);
        System.out.println("Do you want to do add or delete or print or quit");
        String userInput = scanner.next();
        String op1 = scanner.next();
 
        if(op1 == "quit")
        {
            return;
        }
        else if(op1 == "add")
        {
            System.out.println("please type name, yearBorn, phone");
            String input1 = scanner.next();
            String input2 = scanner.next();
            String input3 = scanner.next();
            book.addPerson(Person.input1);
        }
        else if(op1 == "delete")
        {
            System.out.println("please type name of person to be deleted");
            String deleteUserInput = scanner.next();
            removePerson(deleteUserInput);
        }
        else if(op1 == "print")
        {
            book.print();       
        }
        else if(op1 == "quit")
        {
          System.exit(0);
        }
 
    }
}

Open in new window

0
Comment
Question by:ccsk89
  • 5
  • 2
7 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22782634
>>book.addPerson(Person.input1);
>> public void addPerson(Person newPerson)

Since your addPerson takes a Person object, you need to convert your input Strings into a person object.  You will need to do some data validation / conversion as your Person object doesn't take all Strings.

>>public Person(String firstNameX, int yearBornX, String phoneX)

You want something like this:
book.addPerson(new Person("Bob", 2008, "555 555 5555"));

----------

>>removePerson(deleteUserInput);
You just forgot to use the instance, so should be:
book.removePerson(deleteUserInput);
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22782649
Fix those two things and you should be able to compile and test functionality of logic surrounding scanner, etc.
0
 

Author Comment

by:ccsk89
ID: 22782673
First off thank you for your help! I fixed the removePerson method so thanks so much for that.

For the addPerson method you are saying that I need to convert the input1 that the user entered from the scanner into a person object?

   so like:
       Person input1 = new Person(input1)
 
Am I close to what you are saying or am I totally off?

Thanks again!
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22782692
I put code to what I was saying above.

Your constructor for Person takes 3 parameters.  If I understand correctly, input1 is the first name; input2, year of birth; input3, the phone number.  Since year of birth is an integer and not a string, you will have to convert that to a valid int in code but here is the jist.

book.addPerson(new Person(input1, intInput2, input3));

Where intInput2 is the converted int value of input2.
0
 

Author Comment

by:ccsk89
ID: 22782730
OH MY!! you are a lifesaver!! Thank you so so so so much!!!!!  I was thinking that I could only add the first input1 because that was the methods parameters, but I see what you are saying now.  

Again THANKS!!!:)
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22782807
Glad to help.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22782817
<side-note>
I just saw you asked this question earlier, but it was missed.  Since no one commented, you should be able to delete it and save the points to use if you find more issues after running this. :)
</side-note>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet requestdispatcher include and forward differences 1 39
xampp tool 12 56
how to add new optional parameter to JSP 1 49
type mismatch (Object[] to double[] 4 33
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question