HELP! ARRAYS AND SCANNER HELP NEEDED

+1 Jacob Fellows · November 15, 2014
Hi there,

I am building an address book that requires me to use arrays. I have built the array and now need help adding data via the scanner. I am using Netbeans 7.4 IDE. I have built a class called Contact that stores all the String variables under one roof.

Code for class:

package jacobsaddressbook;
/**
 *
 * @author Jacob
 */
public class Contact
{ //Start of class
   private String name;
   private String email;
   private String teleNumber;
public Contact()
    {
    //Making the class public
    }
public Contact(String name, String email, String teleNumber)
    {
    this.name = name;
    this.email = email;
    this.teleNumber = teleNumber;
    }
//Start of the Getters for my main methods
        private String getName()
        {
        return this.name;
        }
        private String getEmail()
        {
        return this.email;
        }
        private String getTeleNumber()
        {
        return this.teleNumber;
        }
//End of the Getters for my main methods
     
//Start of the Setters for my main methods
        private void setName(String n)
        {
         this.name = n;
        }
        private void setEmail(String e)
        {
         this.email = e;
        }
        private void setTeleNumber(String t)
        {
         this.teleNumber = t;
        }
//End of the Getters for my main methods
} //End of class

Code for main program:

package jacobsaddressbook;
import java.util.*;
import java.util.Scanner;
/**
 *
 * @author Jacob
 */
public class JacobsAddressBook
{ //Start of the main class for the main methods
    public static void main(String[] args)
    { //Start of main method
       
        Scanner scan = new Scanner(System.in);
       
        System.out.println("Who's information do you need. Please enter name :: ");
       
       
        // Below is what I need to build the array and get the class infomation from the class file
    Contact[] info;
        
        //Below reserves a total of 50 contacts that can be put into the address book/
    info = new Contact[50];
       
        //Below is how you create the contacts for the address book
        Contact ct1 = new Contact("Jacob", "01865300684", "jff4401@Hotmail.co.uk");
        Contact ct2 = new Contact("Andy", "01212143244", "Andyjonies@gmail.com");
        Contact ct3 = new Contact("","","");
        Contact ct4 = new Contact("","","");
        Contact ct5 = new Contact("","","");
        Contact ct6 = new Contact("","","");
        Contact ct7 = new Contact("","","");
        Contact ct8 = new Contact("","","");
        Contact ct9 = new Contact("","","");
        Contact ct10 = new Contact("","","");
        Contact ct11 = new Contact("","","");
        Contact ct12 = new Contact("","","");
        Contact ct13 = new Contact("","","");
        Contact ct14 = new Contact("","","");
        Contact ct15 = new Contact("","","");
        Contact ct16 = new Contact("","","");
        Contact ct17 = new Contact("","","");
        Contact ct18 = new Contact("","","");
        Contact ct19 = new Contact("","","");
        Contact ct20 = new Contact("","","");
        Contact ct21 = new Contact("","","");
        Contact ct22 = new Contact("","","");
        Contact ct23 = new Contact("","","");
        Contact ct24 = new Contact("","","");
        Contact ct25 = new Contact("","","");
        Contact ct26 = new Contact("","","");
        Contact ct27 = new Contact("","","");
        Contact ct28 = new Contact("","","");
        Contact ct29 = new Contact("","","");
        Contact ct30 = new Contact("","","");
        Contact ct31 = new Contact("","","");
        Contact ct32 = new Contact("","","");
        Contact ct33 = new Contact("","","");
        Contact ct34 = new Contact("","","");
        Contact ct35 = new Contact("","","");
        Contact ct36 = new Contact("","","");
        Contact ct37 = new Contact("","","");
        Contact ct38 = new Contact("","","");
        Contact ct39 = new Contact("","","");
        Contact ct40 = new Contact("","","");
        Contact ct41 = new Contact("","","");
        Contact ct42 = new Contact("","","");
        Contact ct43 = new Contact("","","");
        Contact ct44 = new Contact("","","");
        Contact ct45 = new Contact("","","");
        Contact ct46 = new Contact("","","");
        Contact ct47 = new Contact("","","");
        Contact ct48 = new Contact("","","");
        Contact ct49 = new Contact("","","");
        Contact ct50 = new Contact("","","");
       
        //Below is the way that it adds a Contact to Array(up to 50 people can be added)
        info[0] = ct1; info[1] = ct2; info[2] = ct3 ;info[3] = ct4;
        info[4] = ct5; info[5] = ct6; info[6] = ct7; info[7] = ct8; info[8] = ct9;
        info[9] = ct10; info[10] = ct11; info[11] = ct12;info[12] = ct13;
        info[13] = ct14; info[14] = ct15; info[15] = ct16; info[16] = ct17;
        info[17] = ct18; info[18] = ct19; info[19] = ct20; info[20] = ct21;
        info[21] = ct22; info[22] = ct23; info[23] = ct24; info[24] = ct25;
        info[25] = ct26; info[26] = ct27; info[27] = ct28; info[28] = ct29;
        info[29] = ct30; info[30] = ct31; info[31] = ct32; info[32] = ct33;   
        info[33] = ct34; info[34] = ct35; info[35] = ct36; info[36] = ct37;
        info[37] = ct38; info[38] = ct39; info[39] = ct40; info[40] = ct41;
        info[41] = ct42; info[42] = ct43; info[43] = ct44; info[44] = ct45;   
        info[45] = ct46; info[46] = ct47; info[47] = ct48; info[48] = ct49;
        info[49] = ct50;
       
               
       
    } //End of main method
   
} //End of the main class for the main methods

Please help!
Thanks,
New comer Jacob.

Post a Reply

Replies

Oldest  Newest  Rating
-1 Jacob Fellows · November 16, 2014
xception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: jacobsaddressbook1.ContactListArray.Contact[] cannot be converted to jacobsaddressbook1.ContactListArray.Contact
 at jacobsaddressbook1.ContactListArray.<init>(ContactListArray.java:17)
 at jacobsaddressbook1.JacobsAddressBook1.main(JacobsAddressBook1.java:21)
Java Result: 1

Query:
What does this mean and how do I fix it???

EDIT: Problem is that the code below it doesn't like. All the ContactList things it doesn't like. :
import java.util.ArrayList;
/**
 *
 * @author Jacob
 */
public class ContactListArray
{ //Start of the class java file.
    Contact contactList;
    /***Building the Constructor for the array***\
    *Building the array.
     *
     */
    public ContactListArray()
    { //Start of the public Contact list
        contactList = new Contact[50];
    } // End of the public contact list
    //Adding contact to the list. That way I know how to built the objects \n
    //and will be able to manipulate them in the main method
    public void addContact(String name, String email, String teleNumber)
   {
       for(int index = 0; index < contactList.length; index++){
           if(contactList[index] == null){
               contactList[index] = new Contact(name, email, teleNumber);
               break;
       }
       }
    }
   
    public Contact getContact(String name){
        for(Contact contact: contactList){
            if(contact.getName().equals(name)){
                return contact;
        }
        }
        return null;
    }
    // A method to print all the contacts in the array
    public void printContactList(){
        System.out.println("Name\tTelephone Number\tEmail");
        //Quick note \t = is the way you put a space in the output of a program\n
        //just like when you need to put a space in a word document with tab button.
        for(Contact contact: contactList){
            System.out.println(contact.getName() + "\t" + contact.getTeleNumber() + "\t" + contact.getEmail());  
        }
        }
    //The class that is storing all the strings and teh way they work.
    public class Contact
    { // Start if the contact class that holds the object variables
        private String name;
        private String email;
        private String teleNumber;
   /***Building the constructor for the contact variables***\
    * This will contain my Getters and Setters.
    */
        public Contact(String name, String email, String teleNumber)
        {
            this.name = name;
            this.email = email;
            this.teleNumber = teleNumber;
        }
        public String getName(){
            return this.name;
        }
        public void setName(String n){
            this.name = n;
        }
        public String getEmail(){
            return this.email;
        }
        public void setEmail(String e){
            this.email = e;
        }
        public String getTeleNumber(){
            return this.teleNumber;
        }
        public void setTeleNimber(String t){
            this.teleNumber = t;
        }
               
    }
}

EDIT: FIXED THE ABOVE!!!

Quick query:  how do I remove a person from the array???

Thanks,
Jacob.
0 Marijn van Stee · November 16, 2014
Hi,

It is very easy to change the code to use arrays, here is the code.
I hope it helps you.

import java.util.*;


import tutorials.ContactListArray.Contact;

/**

*

* @author Jacob

*/

public class JacobsAddressBookArray
{ //Start of the main class for the main methods



public static void main(String[] args)
{ //Start of main method

Scanner scan;
ContactListArray list;
String name;
String email;
String telNumber;
Contact contact;

list = new ContactListArray();
scan = new Scanner(System.in);
// add contacts

list.addContact("Jacob", "jff4401@Hotmail.co.uk", "01865300684");
list.addContact("Marijn", "jff4401@Hotmail.co.uk", "01865300684");

System.out.println("Who's information do you need?");
// get the first contact matching the name

do{
System.out.print("Please enter name: ");
name = scan.next();
contact = list.getContact(name);
}while(contact == null);
System.out.println();

System.out.println(contact.getName()+ " " + contact.getEmail() + " " + contact.getTeleNumber());

System.out.print("Add new contact.");
// add a new contact to the list

System.out.print("Enter name: ");
name = scan.next();
System.out.print("Enter email: ");
email = scan.next();
System.out.print("Enter tel.: ");
telNumber = scan.next();
list.addContact(name, email, telNumber);

System.out.println();
// print all the contacts in the list

list.printContactList();

scan.close();
} //End of main method


} //End of the main class for the main methods

public class ContactListArray {

Contact contactList[];


/***CONSTRUCTOR***/

public ContactListArray(){
contactList = new Contact[50];
}

// add contact to the list

public void addContact(String name, String email, String teleNumber){

for(int index = 0; index < contactList.length; index++){
if(contactList[index] == null){
contactList[index] = new Contact(name, email, teleNumber);
break;
}
}
}

// get contact from the list

public Contact getContact(String name){
for(Contact contact: contactList){
if(contact.getName().equals(name)){
return contact;
}
}
return null;
}

// print all contacts

public void printContactList(){
System.out.println("Name\tTelephone Number\tEmail");
for(Contact contact: contactList){
if(contact == null){
break;
}else{
System.out.println(contact.getName()+ "\t" + contact.getTeleNumber() + "\t" + contact.getEmail());
}
}
}



/***INNER CLASS***/

/**

*

* @author Jacob

*/

public class Contact
{ //Start of class

private String name;
private String email;
private String teleNumber;


/***CONSTRUCTOR***/

public Contact(String name, String email, String teleNumber)
{
this.name = name;
this.email = email;
this.teleNumber = teleNumber;
}
//Start of the Getters for my main methods

public String getName()
{
return this.name;
}
public String getEmail()
{
return this.email;
}
public String getTeleNumber()
{
return this.teleNumber;
}
//End of the Getters for my main methods


//Start of the Setters for my main methods

public void setName(String n)
{
this.name = n;
}
public void setEmail(String e)
{
this.email = e;
}
public void setTeleNumber(String t)
{
this.teleNumber = t;
}
//End of the Getters for my main methods

} //End of class


?
}



Groeten,

Marijn
0 Jacob Fellows · November 16, 2014
Hi there,
Thanks for getting back to me. I love the work you have put into it. The only problem is I have to use arrays not arrayslists. But do you think I could get away with using the Arraylist and then converting it to an array or not?

Thanks,
Jacob.
0 Marijn van Stee · November 16, 2014
Hi Jacob,

I think it is better to use the ArrayList, its size is dynamical,
so you can change it at runtime. It has also build in methods.

Just have look at the code, and let me know if it helps you.

import java.util.*;


import tutorials.ContactList.Contact;

/**

*

* @author Jacob

*/

public class JacobsAddressBook
{ //Start of the main class for the main methods



public static void main(String[] args)
{ //Start of main method

Scanner scan;
ContactList list;
String name;
String email;
String telNumber;
Contact contact;;

list = new ContactList();
scan = new Scanner(System.in);
// add contacts

list.addContact("Jacob", "jff4401@Hotmail.co.uk", "01865300684");
list.addContact("Marijn", "jff4401@Hotmail.co.uk", "01865300684");

System.out.println("Who's information do you need?");
// get the first contact matching the name

do{
System.out.print("Please enter name: ");
name = scan.next();
contact = list.getContact(name);
}while(contact == null);
System.out.println();

System.out.println(contact.getName()+ " " + contact.getEmail() + " " + contact.getTeleNumber());

System.out.print("Add new contact.");
// add a new contact to the list

System.out.print("Enter name: ");
name = scan.next();
System.out.print("Enter email: ");
email = scan.next();
System.out.print("Enter tel.: ");
telNumber = scan.next();
list.addContact(name, email, telNumber);

System.out.println();
// print all the contacts in the list

list.printContactList();

scan.close();
} //End of main method


} //End of the main class for the main methods
0 Marijn van Stee · November 16, 2014
Sorry clicked the wrong button, here is the other class


import java.util.ArrayList;

?

public class ContactList {

ArrayList<Contact> contactList;


/***CONSTRUCTOR***/

public ContactList(){
contactList = new ArrayList<Contact>();
}

// add contact to the list

public void addContact(String name, String email, String teleNumber){
contactList.add(new Contact(name, email, teleNumber));
}

// get contact from the list

public Contact getContact(String name){
for(Contact contact: contactList){
if(contact.getName().equals(name)){
return contact;
}
}
return null;
}

// print all contacts

public void printContactList(){
System.out.println("Name\tTelephone Number\tEmail");
for(Contact contact: contactList){
System.out.println(contact.getName()+ "\t" + contact.getTeleNumber() + "\t" + contact.getEmail());
}
}



/***INNER CLASS***/

/**

*

* @author Jacob

*/

public class Contact
{ //Start of class

private String name;
private String email;
private String teleNumber;


/***CONSTRUCTOR***/

public Contact(String name, String email, String teleNumber)
{
this.name = name;
this.email = email;
this.teleNumber = teleNumber;
}
//Start of the Getters for my main methods

public String getName()
{
return this.name;
}
public String getEmail()
{
return this.email;
}
public String getTeleNumber()
{
return this.teleNumber;
}
//End of the Getters for my main methods


//Start of the Setters for my main methods

public void setName(String n)
{
this.name = n;
}
public void setEmail(String e)
{
this.email = e;
}
public void setTeleNumber(String t)
{
this.teleNumber = t;
}
//End of the Getters for my main methods

} //End of class


?
}

Groeten,
Marijn
  • 1

Java / Android Development

107,266 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator