Results 1 to 5 of 5
  1. #1
    Renthalkx is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Method can't access arrays

    Hi,
    I'm new to Java coming from C++ so it hasn't been terribly difficult to learn but I've run into a problem. I'm making a phonebook and the problem is I that so far I have an AddContact and a ViewContact method. The AddContact works fine but ViewContact gives me an error when I type in the contact to search for. What's weird though is if that I move the viewContact code to the AddContact and just make it so it runs directly after what addContact does it works fine. So what I am thinking is that ViewContact doesn't have access to the arrays to get the information so when I search or a name it gets the error. I'm not really sure. I've tried declaring the arrays outside of everything else so there public but still doesn't work. Here's my code:
    Java Code:
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args)
    	{
    		new Menu();
    	}
    	
    	
    }
    Java Code:
    import java.util.Scanner;
    
    public class Menu {
    	public Menu() {	
    	System.out.println("\tPhonebook");
    	System.out.println("Main Menu");
    	System.out.println("Enter the name of the item that you want to view");
    	System.out.println("Add\tRemove\tEdit\tView\tExit");
    	
    	boolean running = true;
    	while(running = true){
    		Scanner MenuInput = new Scanner(System.in);
    		String MenuChoice = MenuInput.nextLine();
    
    		switch(MenuChoice) {
    		case "Add": 
    			Contacts ContactsObject = new Contacts();
    			ContactsObject.addContacts();
    			break;
    		case "View":
    			Contacts ViewObject = new Contacts();
    			ViewObject.viewcontacts();
    			break;
    		case "Exit":
    			System.out.println("Phonebook closed.");
    				System.exit(0);
    		default:
    			System.out.println("Invalid Choice. Please try again");
    			break;
    		}
    	}
    	}
    }
    Java Code:
    import java.util.Scanner;
    
    
    public class Contacts {
    	
    	String contactName[] = new String[2];
    	String contactNumber[] = new String[2];
    	String contactType[] = new String[2];
    	
    	public void addContacts() {
    		
    		Scanner inputName = new Scanner(System.in);
    		
    		
    		Scanner inputNumber = new Scanner(System.in);
    		
    		
    		Scanner inputType = new Scanner(System.in);
    		
    		for(int i=0;i < contactName.length;i++) {
    			System.out.println("Enter Name: ");
    			contactName[i] = inputName.nextLine();
    			System.out.println("Enter number: ");
    			contactNumber[i] = inputNumber.nextLine();
    			System.out.println("Enter type: ");
    			contactType[i] = inputType.nextLine();
    		}
    		
    		System.out.println("\nName\tNumber\t\tType");
    		for(int i=0;i < contactName.length;i++) {
    			System.out.println(contactName[i] + "\t" + contactNumber[i] + "\t" + contactType[i]);
    		}
    	}
    	
    	public void viewcontacts() {
    		System.out.println("Name of contact to edit:");
    		Scanner inputContactEdit = new Scanner(System.in);
    		String contactEdit = inputContactEdit.nextLine();
    		for(int i=0;i < contactName.length;i++) {
    			if(contactName[1] .equals(contactEdit)) {
    				System.out.println("Found");
    				System.out.println("\nName\tNumber\t\tType");
    				System.out.println(contactName[i] + "\t" + contactNumber[i] + "\t" + contactType[i]);
    				}
    			}
    	}
    	
    }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Method can't access arrays

    Hi Renthalkx, welcome to the forums.

    For a start your code on line 40 only checks the input against the String in contactName[1].

    Each time you using the keyword 'new' it creates a new instance of the object which is stored at a different memory address. On both lines 17 and 21 you create new instances of Contacts which do not result in the same object.
    Instead you could define a single array or list called Phonebook which stores a number of Contacts. When you what to add a new contact you would add a new contact to the array. When you want to view a contact or all contacts you would reference an instance within Phonebook.

    Regards.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Method can't access arrays

    A couple of things.

    1. Line 11, class Menu: Change "while (running = true)" to "while(running == true)"
    2. Line 40, class Contacts: Change you index from 1 to i.
    3. The main problem is that you create a new object of contacts when you enter view mode or any other mode This is a totally different object than the "other" object where you entered the data.

    I would recommend a slight design change. Why not create a Contact class which holds the information for each contact? Then you can have a single array of type Contact.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Method can't access arrays

    You would also be better off using a single scanner instead of one for each field.

    Regards.

  5. #5
    Renthalkx is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: Method can't access arrays

    How do I go about storing the information to each array if i only use one scanner?
    EDIT:
    Nevermind got it

Similar Threads

  1. Replies: 2
    Last Post: 02-13-2012, 07:39 AM
  2. why i cant access a method in a sub class?
    By javanew in forum New To Java
    Replies: 7
    Last Post: 05-03-2010, 07:29 PM
  3. add method in arrays
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 03-14-2009, 12:58 PM
  4. multiple threads access the same method.
    By bhanu in forum New To Java
    Replies: 3
    Last Post: 02-16-2009, 07:54 AM
  5. Method access or field access
    By carderne in forum New To Java
    Replies: 2
    Last Post: 12-06-2008, 07:20 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
  •