im getting an error while trying to print array contacts, why? any help? Thanks! :)

this is my class Address where im gettingthe error:

import java.io.*;
import java.util.*;
  public class Address{

      //Declare variables
      String first;
	  String last;
	  String homeadd;
	  String homeph;
	  String cellnum;

		Scanner sc =  new Scanner(System.in);

	  public Address(String firstName, String lastName, String homeAddress, String homePhone, String cellNumber){
	  	first=firstName;
	  	last=lastName;
	  	homeadd=homeAddress;
	  	homeph=homePhone;
	  	cellnum=cellNumber;
	  }

	  public void printMenu(){
		System.out.println("1. Add a person to the address book.");
		System.out.println("2. See if a person is in the address book.");
		System.out.println("3. Print the address book.");
		System.out.println("4. Save the address book to disk.");
		System.out.println("5. Load the address book from disk.");
		System.out.println("6. Terminate the program.");

		int answer = sc.nextInt();

	//Switch to navigate menu
	switch (answer){
		case 1: getaddress();
		printMenu();
		break;
		case 2: //make method to search then print yes/no
		break;
		case 3: print(); //make a method to print
		printMenu();
		break;
		case 4: getaddress(); //getaddress is unfinished
		break;
		case 5: getbook();
		break;
		case 6: System.exit(0);
		break;
		default: System.out.println("Enter a number between 1 and 6.");
		printMenu();
	}
}

      Address(){
      }

		double contacts[];
      //Get address
      public void getaddress(){
		  System.out.println("How many contacts do you want to add?");
		  int number= sc.nextInt();
		  contacts = new double [number];
		  for( int i = 1; i<=number; i++){
		  System.out.println();
		  System.out.println("Enter contact "+i+ " first name:");
		  first = sc.next();
		  //outFile.print(first);
		  System.out.println("Enter contact "+i+ " last name:");
		  last = sc.next();
		  //outFile.print(last);
		  System.out.println("Enter contact " +i+ " home address.");
		  homeadd = sc.next();
		  //outFile.print(homeadd);
		  System.out.println("Enter contact " +i+ " home phone");
		  homeph = sc.next();
		  //outFile.print(homeph);
		  System.out.println("Enter contact "+i+ " cellphone");
		  cellnum = sc.next();
		  //outFile.print(cellnum);
	  }}

	  public void print(){
		  System.out.println("Address Book Contacts   "+contacts[i]);
	  }





	  public void getbook(){
		  //Declare variables
		  String infile;
		  String outfile;

		  //Get file locations
		  System.out.println("Enter location of the address book");
		  System.out.println("An example of this is C:hello.txt");
		  infile = sc.next();
		  System.out.println("Enter location of the output file.");
		  outfile = sc.next();
		  }
	  }

and this is my class Project:

import java.io.*;
import java.util.*;
   class Project{
	   static Scanner sc = new Scanner(System.in);


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

		   //Call constructor and set those variables as null
		   Address addressObject = new Address();
		   addressObject.first = "null";
		   addressObject.last = "null";
		   addressObject.homeadd = "null";
		   addressObject.homeph = "null";
		   addressObject.cellnum = "null";





		   //Menu print
		   System.out.println("1. Add contacts to the address book.");
		   System.out.println("2. See if a person is in the address book.");
		   System.out.println("3. Print the address book.");
		   System.out.println("4. Save the address book to disk.");
		   System.out.println("5. Load the address book from disk.");
		   System.out.println("6. Terminate the program.");


		   //Get data member to navigate menu
			answer = sc.nextInt();
		   //Switch to navigate menu
		   switch (answer){
			   case 1: addressObject.getaddress();
			   addressObject.printMenu();
			   break;
			   case 2: //make method to search then print yes/no
			   break;
			   case 3: addressObject.getbook(); //make a method to print
			   break;
			   case 4: addressObject.getaddress(); //getaddress is unfinished
			   break;
			   case 5: addressObject.getbook();
			   break;
			   case 6: System.exit(0);
			   break;
			   default: System.out.println("Enter a number between 1 and 6.");
		   }
	   }
   }

Recommended Answers

All 12 Replies

public void print(){
System.out.println("Address Book Contacts "+contacts[i]);
}

Where do you define 'i' at the above code? That is why you get the error.

Also you will have many Addresses at your program. Meaning that you will have a collection of Addresses. So why do you put the printMenu and functionality in that class.
Have this:

public class Address{

      //Declare variables
      String first;
	  String last;
	  String homeadd;
	  String homeph;
	  String cellnum;


	  public Address(String firstName, String lastName, String homeAddress, String homePhone, String cellNumber){
	  	first=firstName;
	  	last=lastName;
	  	homeadd=homeAddress;
	  	homeph=homePhone;
	  	cellnum=cellNumber;
	  }

public Address() {
}

// have methods for getting and setting the value of the class

In another class, in the main method you will have your menu and you will create instances of the class Address

i actually changed the code to this, is it fine?

import java.io.*;
import java.util.*;
   class Project{
	   static Scanner sc = new Scanner(System.in);

	   public static void main(String[] args)throws IOException{
		   int answer;
		   answer = 0;

		   //Call constructor and set those variables as null
		   Address addressObject = new Address();
		   addressObject.first = "null";
		   addressObject.last = "null";
		   addressObject.homeadd = "null";
		   addressObject.homeph = "null";
		   addressObject.cellnum = "null";


		   //Menu print
		   System.out.println("***Address Book Menu***");
		   System.out.println("1. Add contacts to the address book.");
		   System.out.println("2. See if a person is in the address book.");
		   System.out.println("3. View contacts in the address book.");
		   System.out.println("4. Terminate the program.");


		   //Choose number to navigate menu
			answer = sc.nextInt();

		   //Switch to navigate menu
		   switch (answer){
			   case 1: System.out.println();
			   addressObject.getaddress();
			   addressObject.printMenu();
			   break;
			   case 2: //make method to search then print yes/no
			   break;
			   case 3: System.out.println();
			   addressObject.print();
			   System.out.println();
			   addressObject.printMenu();
			   break;
			   case 4: System.exit(0);
			   break;
			   default: System.out.println("Enter a number between 1 and 4.");
		   }
	   }
   }

and,

import java.io.*;
import java.util.*;
  public class Address{

      //Declare variables
      String first;
	  String last;
	  String homeadd;
	  String homeph;
	  String cellnum;

		Scanner sc =  new Scanner(System.in);

	  public Address(String firstName, String lastName, String homeAddress, String homePhone, String cellNumber){
	  	first=firstName;
	  	last=lastName;
	  	homeadd=homeAddress;
	  	homeph=homePhone;
	  	cellnum=cellNumber;
	  }

	  public void printMenu(){
		  System.out.println("***Address Book Menu***");
		  System.out.println("1. Add a person to the address book.");
		  System.out.println("2. See if a person is in the address book.");
		  System.out.println("3. View contacts in the address book.");
		  System.out.println("4. Terminate the program.");

		  int answer = sc.nextInt();

	//Switch to navigate menu
	switch (answer){
		case 1: getaddress();
		printMenu();
		break;
		case 2: //make method to search then print yes/no
		break;
		case 3:print();
		printMenu();
		break;
		case 4: System.exit(0);
		break;
		default: System.out.println("Enter a number between 1 and 4.");
		printMenu();
	}
}

      Address(){
      }


      //Get address
      public void getaddress(){
		  String first[];
		  String last[];
		  String home[];
		  String phone[];
		  String cell[];

		  PrintWriter outputStream = null;
		  			try{
		  				outputStream = new PrintWriter(new FileOutputStream("out.txt", true));
		  			}
		  			catch(FileNotFoundException e){
		  				System.out.println("Error opening the fileout.txt.");
		  				System.exit(0);
			}

		  System.out.println("How many contacts do you want to add?");
		  int number= sc.nextInt();
		  first = new String [number];
		  last = new String [number];
		  home = new String [number];
		  phone = new String [number];
		  cell = new String [number];

		  for( int i = 0; i<number; i++){
		  System.out.println();
		  System.out.println("Enter contact "+(i+1)+ " first name:");
		  first[i] = sc.next();
		  outputStream.println();
		  outputStream.println("First name: "+first[i]);
		  System.out.println("Enter contact "+(i+1)+ " last name:");
		  last[i] = sc.next();
		  outputStream.println("Last name: "+last[i]);
		  System.out.println("Enter contact " +(i+1)+ " home address.");
		  home[i] = sc.next();
		  outputStream.println("Home Address: "+home[i]);
		  System.out.println("Enter contact " +(i+1)+ " home phone");
		  phone[i] = sc.next();
		  outputStream.println("Home Number: "+phone[i]);
		  System.out.println("Enter contact "+(i+1)+" cellphone");
		  cell[i] = sc.next();
		  outputStream.println("Cell Phone Number: "+cell[i]);
		  System.out.println();


	  }
	  outputStream.close();
	  }

	      public void print(){
		 		  System.out.println("Address Book Contacts");
		 		  System.out.println("                     ");
		 		  //Reading from a .txt file.
		 		  File file = new File("C:\\myclasses\\iiiiii\\out.txt");
		 		  FileInputStream fis = null;
		 		  DataInputStream dis = null;

		 		  try {
					  fis = new FileInputStream(file);
					  dis = new DataInputStream(fis);
					  // dis.available() returns 0 if the file does not have more lines.
					  while (dis.available() != 0) {
						  // this statement reads the line from the file and print it to the console.
						  System.out.println(dis.readLine());
					  }
					  // dispose all the resources after using them.
					  fis.close();
					  dis.close();
					  }
					  catch (FileNotFoundException e) {
						  e.printStackTrace();
					  }
					  catch (IOException e) {
						  e.printStackTrace();
					  }





}}

and about the case 3, how can i make a method to search in the txt file and print yes/no ?? any ideas??? Thanks! :D

Since you don't seem to get my suggestions I prepared this small document on how to proceed:

The Address class will have ONLY these:

class Address {
  private String first = "";
  // rest attributes last, homeadd ...
  
  public Address() {
  }
  
  public String getFirst() {
    return first;
  }
  public void setFirst(String first) {
    this.first = first;
  }
  
  // rest methods for last, homeadd ...
}

Read the class Vector. Look at the methods add, get, size

class AddressBook {
  public Vector addresses = new Vector();
  
  public AddressBook() {
  
  }
}

And in the main class

public class Main() {
  public static void main(String [] args) {
      Scanner scan = new Scanner(System.in);
      AddressBook book = new AddressBook();
      String choice = "";
      
      
      // print menu
      ....
      //
      choice = scan.nextLine();
      
      if (choice.equals("1") { // add contacts
        Address ad = new Address();
        // read the data from the keyboard (scan.nextLine())
        // use the set methods (ad.setFirst(...)
        book.addresses.add(ad);
        
      } else if (.. "2") { // See if a person is in the address book
        // read the name from the keyboard
        // loop the Vector and take each Address. Then compare the name of each Address with the one given
        
      } else if (.. "3") { // View contacts in the address book
        // loop the Vector, take each Address and print its values
      
      } else if (.. "4") { // Exit
        
      } else {
        System.out.println("Error choice");
      }
  }
}

You might want to put the part form the "print menu" till the "if statements" into a while loop in order to add multiple Addresses and repeat the proccess:

while (!choice.equals("4") {

}

can u give me some code that helping me making simple addressbook?

thats have output

attributes | description
name |
address |
telnum |
email add |

using util scanner

and provide nesscesarry accessor and mutator methods all the attributes

and creating object provide
add entry, delete entry view entries,, update entry

can u give me some code that helping me making simple addressbook?

thats have output

attributes | description
name |
address |
telnum |
email add |

using util scanner

and provide nesscesarry accessor and mutator methods all the attributes

and creating object provide
add entry, delete entry view entries,, update entry

Start a new thread with your question and your code.
As far as your question:
since you posted in this thread, you should have read it and saw that your question has been answered. This thread has the code and examples that you need to get you started.

give me the source code to my e-mail id

commented: That is not how this forum works!! -1

give me the source code to my e-mail id

Read the previous posts. Start a new thread.
And we don't give away homework or give code through emails or PMs. It is against the rules to post your email asking for help.

If you want help start a new thread with specific questions.

fgfgfgjgjdhtjghdhtjdgjgjghj
commented: And what is this suppose to be? -1
import javax.swing.*;

  public class addressbook {

  public static void main(String []args){

  }



  private static double term;

  private static String[] Name;

  private static String[] Address;

  private static int[] Telephone;

  private static String[] Email;

  private static int Entry=0;

  private static double Add;

  private static double View;

  private static double Delete;




  public static double getTerm(){



  double AddEntry=0;

  double DeleteEntry=0;

  double ViewEntry=0;

  double UpdateEntry=0;

  double exit=0;

 Name=new String[20];

 Address=new String[20];

 Telephone=new int[20];

  Email=new String[20];


  final int MAX_SIZE=5;

  int[] arrOptions = new int[20];



  for(int i=5;term<5;i=5){

  term = Double.parseDouble(JOptionPane.showInputDialog( "Please, Select Transaction\n [1]Add Entry\n [2]Delete Entry\n [3]View All Entries\n [4]Update Entry\n [5]Exit", "Address Book"));

 if(term==1){

  AddEntry=addressbook.getEntry();

  }

  if(term==2){

 DeleteEntry=addressbook.getDelete();

 }

  if(term==3){

 ViewEntry=addressbook.getView();

  }

 if(term==4){

 UpdateEntry=addressbook.getEntry();

  addressbook.getView();

  }

  if(term==5){

  exit=addressbook.getExit();

  }

  }

  return term;
  }

 public static double getEntry(){

  Entry = (int) Double.parseDouble(JOptionPane.showInputDialog("Enter the entry no. Select from 1-10: "))-1;



  Name[Entry] = JOptionPane.showInputDialog(null, "Enter your Name:",JOptionPane.INFORMATION_MESSAGE);

 Address[Entry] = JOptionPane.showInputDialog(null, "Enter your Address:",JOptionPane.INFORMATION_MESSAGE);

  Telephone[Entry] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your Telephone No. :",JOptionPane.INFORMATION_MESSAGE));

  Email[Entry] = JOptionPane.showInputDialog(null, "Enter your Email Address:",JOptionPane.INFORMATION_MESSAGE);


 return term;

  }



  public static double getDelete(){

  try{

 Entry = (int) Double.parseDouble(JOptionPane.showInputDialog("En ter the entry you want to delete:"));

 if(Entry<11&&Entry>0){



  Name[Entry-1]="";

  Address[Entry-1]="";

 Telephone[Entry-1]=0;

  Email[Entry-1]="";


  JOptionPane.showMessageDialog(null,"Entry deleted!");

  }

  else{

 JOptionPane.showMessageDialog(null, "Invalid Input");

  }

 }

  catch(Exception e){

 JOptionPane.showMessageDialog(null, "Please try again.","Error",JOptionPane.ERROR_MESSAGE);

  System.exit(0);

 }

 return Delete;

  }

 public static double getView(){
  try{

  for(Entry=0;9>Entry;Entry=Entry+1){

 JOptionPane.showMessageDialog(null, "Name: "+Name[Entry]+"\n Address: "+Address[Entry]+"\n Telephone #: "+Telephone[Entry]+"\n Email Address: "+Email[Entry],"\n",JOptionPane.PLAIN_MESSAGE);

  }



  return View;
  }

  catch(Exception e){

 JOptionPane.showMessageDialog(null, "Please try again.","Error",JOptionPane.ERROR_MESSAGE);

 System.exit(0);

  }

  return View;

  }

  public static double getExit() {

  double exit=0;

 JOptionPane.showMessageDialog(null, "Exit");

  System.exit(0);

  return exit;
 }
  }


[B][I]please help me..i dont have errors but i dont have any output.[/I]...[/B]
fgfgfgjgjdhtjghdhtjdgjgjghj

Start a new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.