Results 1 to 5 of 5
Thread: Problems calling a method.
- 10-10-2012, 04:11 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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"); } }
-
Re: Problems calling a method.
Have a look here:
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.Java Code:private int count = 0; private String[] contact = new String[count]; private String[] number = new String[count];
- 10-10-2012, 05:35 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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!
-
Re: Problems calling a method.
You're welcome and good luck!
- 10-10-2012, 04:56 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Problems calling methods with arrays.
By pyat77 in forum New To JavaReplies: 3Last Post: 11-02-2011, 01:06 AM -
Problems calling JPanel
By scheffetz in forum New To JavaReplies: 4Last Post: 04-14-2011, 07:28 PM -
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 11:40 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
Problems with readLine() and calling methods
By peachyco in forum New To JavaReplies: 2Last Post: 11-24-2007, 07:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks