Results 1 to 5 of 5
  1. #1
    RadicalNH is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Problems calling a method.

    New to the forum, forgive me If im not posting my code correctly or giving enough detail. Im trying to call the readFile() method in the run() method and its giving me the error
    Name of Phonebook file to read in: myBook.txt
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Phonebook2.readFile(Phonebook2.java:28)
    at Phonebook2.run(Phonebook2.java:36)
    at Homework3.main(Homework3.java:8)

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Phonebook2
    {  
      private File file;
      private int count = 0;
      private String[] contact = new String[count]; 
      private String[] number = new String[count];
      
      public void readFile() throws IOException
      {
        Scanner scan;
    	scan = new Scanner(System.in);
        System.out.print("Name of Phonebook file to read in: "); 
    	String txtFile = scan.next(); 
    	file = new File(txtFile); 
    	Scanner fileScan = new Scanner (file); 
        count = fileScan.nextInt(); 
    	
    	for (int i=0; i<count; i++)
    		{
    		  contact[i] = fileScan.next();
    		  number[i] = fileScan.next();
    		}	  
         System.out.println("Phonebook Successfully Read in!");
      }
      
      public void run() throws IOException
      { 
        readFile();
        Scanner scan;
    	scan = new Scanner(System.in);
        String query ="";
    	do{
    	    System.out.print("Please enter a search query: ");
    		query = scan.next();
    		if (query.startsWith("^")) 
    	      linearSearch(query);  
    			
    		if (!query.equals("*") && !query.startsWith("^"))
    		    binarySearch(query);
    	 } while(!query.equals("*"));
        System.out.print("Thank you for using this program!");
      }
      
      //Linear search Method
      public void linearSearch(String query) 
      {
    	int found = 0; 
    	String contactStr ="", numberStr ="";
    	query = query.replace("^",""); 
    	
    	for (int i =0; i<count; i++)
    	{
    	  contactStr = contact[i];
    	  numberStr = number[i];
    	  
    	  if (contactStr.toLowerCase().startsWith(query.toLowerCase()) || contactStr.toLowerCase().endsWith(query.toLowerCase())){
    	    System.out.println(contactStr + " " + contactStr); 
            found = 1;
            break;	 
    	  }
    	}
        if (found == 0)
          System.out.println("***No Entry Found***");	
      }
      
      //Binary Search Method
      public void binarySearch(String query)
      {
        System.out.println("Binary search under construction");
      }
      
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problems calling a method.

    Have a look here:
    Java Code:
      private int count = 0;
      private String[] contact = new String[count]; 
      private String[] number = new String[count];
    count is 0 when you create your contact and number arrays, and so these arrays will only and always have 0 sizes meaning that they can hold no items at all. I suggest that yes, you declare your arrays at this location, but that you don't create new array objects until *after* you get a valid count value.

  3. #3
    RadicalNH is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Problems calling a method.

    Thanks for help, that makes sense. Wish I would have thought of that. On my nexus right now but will try to debug in the morning, thanks again!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    RadicalNH is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Problems calling a method.

    Thanks it works now, I also closed a scan to early which also made it not work, eclipse is complaining about it being a resource leak. But im not really concerned about that right now hah.

Similar Threads

  1. Problems calling methods with arrays.
    By pyat77 in forum New To Java
    Replies: 3
    Last Post: 11-02-2011, 01:06 AM
  2. Problems calling JPanel
    By scheffetz in forum New To Java
    Replies: 4
    Last Post: 04-14-2011, 07:28 PM
  3. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. Problems with readLine() and calling methods
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 07:44 AM

Posting Permissions

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