Results 1 to 8 of 8
Thread: Array Printing out jiberish
- 10-16-2012, 10:00 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Array Printing out jiberish
Im trying to make a program to read in a file into an array called listing with the variables name and number in it. I made a separate class called listing that contains those variables. I did this to try and avoid a parallel array which causes problems with my search methods. I dont understand why my readFile() method isnt working and is just printing out nonsense. Thanks for the help
Here is my phonebook class Code at the bottom is my listing class code
Listing Class codeJava Code:public class Phonebook2 { private File file; private int count = 0; private Listing[] contact; public void readFile(String txtFile) throws IOException { file = new File(txtFile); Scanner fileScan = new Scanner (file); count = fileScan.nextInt(); contact = new Listing[count]; for (int i=0; i<count; i++) { String name = fileScan.next(); String number = fileScan.next(); contact[i] = new Listing(name, number); System.out.println(contact[i]); } fileScan.close(); } public void run() throws IOException { Scanner scan = new Scanner(System.in); String query; System.out.print("Name of Phonebook file to read in: "); String txtFile = scan.next(); readFile(txtFile); System.out.println("Phonebook Successfully Read in!"); do{ System.out.print("Please enter a search query: "); query = scan.next(); query = query.toLowerCase(); if (query.startsWith("^")) linearSearch(query); if (!query.equals("*") && !query.startsWith("^")) binarySearch(query); } while(!query.equals("*")); System.out.print("Thank you for using this program!"); scan.close(); } /*Linear Search Method * */ public void linearSearch(String query) { boolean found = false; String contactStr =""; //numberStr = ""; query = query.replace("^",""); for (int i =0; i<count; i++) { //contactStr = contact[i]; if (contactStr.toLowerCase().startsWith(query) || contactStr.toLowerCase().endsWith(query)){ System.out.println(contactStr); found = true; break; } } if (!found) System.out.println("***No Entry Found***"); } /*Binary Search Method * */ public void binarySearch(String query) { /* Comparable result = null; int first = 0, last = contact.length - 1, mid; while (result == null && first <= last) { mid = (first + last) / 2; if(contact[mid].toLowerCase().startsWith(query)) result = contact[mid]; else if(contact[mid].compareTo(query) > 0) last = mid -1; else first = mid + 1; } if(result == null) System.out.println("No Entry Found!"); else System.out.println(result); */ } }
Java Code:import java.io.*; public class Listing { private String name, number; public Listing (String name, String number) { this.name = name; this.number = number; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setNumber(String number) { this.number = number; } public String getNumber() { return number; } public Listing() { this("New Name", "New Number"); } }
- 10-16-2012, 10:59 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Array Printing out jiberish
As regards the output, what does it print? And what did you expect or intend it would print?I dont understand why my readFile() method isnt working and is just printing out nonsense.
Much the same with the "isn't working": what does it do, and what did you expect or intend it would do? I am guessing that you call run() from somewhere, but it might be as well to completely describe the problem by saying, or, better, adding a main() method so the code will run.
- 10-16-2012, 11:06 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Array Printing out jiberish
Sorry for the lack of details, run is called in another class. Ill post it. It is supposed to print out a list of names and phone numbers taken in from a file called myBook.txt which should look like:
Tom-Arnold 603-641-4131
Jessica-Alba 603-641-4127
Billy-Exterminator 603-641-4144
John-Wayen 603-641-4352
Bart-Simpson 603-641-4323
instead I get this printed out when i print contact[i] in the for loop
Listing@570c16b7
Listing@5aa77506
Listing@27f40b69
Listing@7192efd
Listing@65be7af
Java Code:// This is the main class. Do not modify. import java.io.*; public class Homework3 { public static void main(String[] args) throws IOException { Phonebook2 book = new Phonebook2(); book.run(); } }
- 10-16-2012, 11:15 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Array Printing out jiberish
What you see is the default string representation of an array (which is an object) resulting from calling the toString method (see Object.toString() method in the API for a description of what this actually represents). To print out the array, you can use Arrays.toString(myArray), or loop over the array and explicitly print the contents.
- 10-17-2012, 12:02 AM #5
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Array Printing out jiberish
Ive tried looping through the array and using the Arrays.toString(contact) but i still get the same Listing@570c16b7
Listing@5aa77506
Listing@27f40b69
Listing@7192efd
Listing@65be7a
- 10-17-2012, 12:22 AM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Array Printing out jiberish
Did you read the API of the toString method of Object I posted above? The class you posted does not override this method, so it will print the default value returned by toString().
- 10-17-2012, 12:51 AM #7
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Array Printing out jiberish
Thanks I got it working now. Not sure exactly whats going on in the code. But I added this to the listing class.
and now it works. Need to do some more research on the toString(). Does it automatically get called because i was trying to make an object into a string? Just not sure whats going on there.Java Code:public String toString() { return name + "\t" + number; }
- 10-17-2012, 01:13 AM #8
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Array Printing out jiberish
Yes. Calling System.print on an object ends up calling the toString() method of the passed object. This is outlined in the API for PrintStream (which in turn calls String.valueOf())
Similar Threads
-
Printing 2D Array Error
By noble in forum New To JavaReplies: 9Last Post: 11-09-2010, 05:31 PM -
Printing an Array
By overlordjebus in forum Java AppletsReplies: 9Last Post: 04-14-2010, 02:26 AM -
Problem printing an array
By denial in forum New To JavaReplies: 3Last Post: 11-01-2009, 10:09 PM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
printing array
By mayhewj7 in forum New To JavaReplies: 6Last Post: 02-12-2009, 02:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks