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

    Default How to convert from object to String

    My phonebook program imports a text file with a list of names and phonenumbers. It stores them into an array made with the class listing which has two variables name and numbers. I was able to create the array and print it out fine. But when it comes to try to use my search algorithm on it I cant figure out how to, or even if its possible. I get the errors Multiple markers at this line
    - Type mismatch: cannot convert from Listing
    to String
    - Syntax error, insert ";" to complete
    BlockStatements
    - Cannot cast from Listing to String
    Feel like im close to getting this project done!
    Here is my code. Thanks for the help!
    Phonebook2 class this class contains the readfile nad searches
    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);
    		}
    	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 nameStr = ""; 
    	query = query.replace("^","");
    	for (int i =0; i<count; i++)
    	{ 
    	  nameStr = contact[i]; /////////////////////////ERROR IS HERE
    	  if (nameStr.toLowerCase().startsWith(query) || nameStr.toLowerCase().endsWith(query)){
    	    System.out.println(nameStr); 
            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);
    	      */
      } 
    }
    The listing class used to help setup the array and avoid having to use a parallal array for name and number
    Java Code:
    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");
      }
      
      public String toString()
      {
        return name + "\t" + number;
      }
    
    
    }
    Just calls the method run in Phonebook2
    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();
      }
    }
    Last edited by RadicalNH; 10-17-2012 at 11:37 PM.

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

    Default Re: How to convert from object to String

    Java Code:
    nameStr = contact[i];
    contact is declared as an array of Listing. (listings would have been a more descriptive name, but anyway...)

    And that means that contact[i] is a reference to a Listing.

    But nameStr was declared as a String variable! What the compiler is getting at is that you can't assign a Listing to a String. (Think of variables as containers: a Listing is basically the wrong shape and/or size for a String container. Java is very fussy about this. The reason for declaring nameStr as a String in the first place was so that the compiler could check the sorts of things you were assigning to it.)

    So what string did you intend nameStr to be? Sorry, that's a silly question, even if it is the logical one to ask. Assuming that you meant nameStr to be the name of the Listing contact[i] then you should use the Listing method you have written to obtain that name.

    Java Code:
    nameStr = contact[i].???();

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

    Default Re: How to convert from object to String

    Yeha listing would have been a better name... I made the class listing after I already had contact[] array. Ill change that first. the nameStr was meant to change the contact[i] array into a string array so I could use the string methods in my search. How would I go about usingthe listing method in that searc

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

    Default Re: How to convert from object to String

    nameStr was meant to change the contact[i] array into a string array
    But contact[i] is a Listing, not an array of any sort! If you want to obtain a String from a Listing (no array involved) then use contact[i].???() as suggested above. Where ??? is one of the methods that returns a String from a Listing.

    The linear search strategy is something like this:

    Java Code:
    you start with an array of listings
    you walk through from the start to the end (hence it's linear)
    and for each listing you meet:
        you obtain a string from the listing (*)
        if that string has a certain condition:
            set found to true
    finally report the value of found
    Notice how the line marked (*) obtains a string from a listing. No array is involved with this step. The Listing methods are there to perform this step.

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

    Default Re: How to convert from object to String

    Oh I thought contact[i] was an array of listing. This is starting to make sense. Ill report back soon! T

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

    Default Re: How to convert from object to String

    Got everything working. Thanks for the help!

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

Similar Threads

  1. How do I convert a String[] object to a Set?
    By RPFeltz in forum New To Java
    Replies: 3
    Last Post: 08-20-2011, 04:15 PM
  2. Convert object to String
    By vinnie in forum New To Java
    Replies: 10
    Last Post: 12-12-2010, 05:45 AM
  3. Convert object to String
    By innspiron in forum New To Java
    Replies: 3
    Last Post: 03-14-2010, 01:26 PM
  4. How to convert string array into object in java
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 02-12-2010, 12:33 PM
  5. convert a string to an object in java
    By jforce93 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 11:57 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
  •