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

    Default 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
    Java 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);
    	      */
      } 
      
    }
    Listing Class code
    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");
      }  
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array Printing out jiberish

    I dont understand why my readFile() method isnt working and is just printing out nonsense.
    As regards the output, what does it print? And what did you expect or intend it would print?

    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.

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

    Default 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();
      }
    }

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default 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.

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

    Default 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

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default 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().

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

    Default 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.
    Java Code:
    public String toString()
      {
        return name + "\t" + number;
    	     }
    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.

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Array Printing out jiberish

    Quote Originally Posted by RadicalNH View Post
    Does it automatically get called because i was trying to make an object into a string?
    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

  1. Printing 2D Array Error
    By noble in forum New To Java
    Replies: 9
    Last Post: 11-09-2010, 05:31 PM
  2. Printing an Array
    By overlordjebus in forum Java Applets
    Replies: 9
    Last Post: 04-14-2010, 02:26 AM
  3. Problem printing an array
    By denial in forum New To Java
    Replies: 3
    Last Post: 11-01-2009, 10:09 PM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  5. printing array
    By mayhewj7 in forum New To Java
    Replies: 6
    Last Post: 02-12-2009, 02:01 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
  •