Results 1 to 7 of 7
- 10-17-2012, 11:31 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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
The listing class used to help setup the array and avoid having to use a parallal array for name and numberJava 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); */ } }
Just calls the method run in Phonebook2Java 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; } }
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.
- 10-17-2012, 11:45 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: How to convert from object to String
contact is declared as an array of Listing. (listings would have been a more descriptive name, but anyway...)Java Code:nameStr = contact[i];
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].???();
- 10-17-2012, 11:56 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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
- 10-18-2012, 12:34 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: How to convert from object to String
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.nameStr was meant to change the contact[i] array into a string array
The linear search strategy is something like this:
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.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
- 10-18-2012, 12:38 AM #5
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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
- 10-18-2012, 04:25 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: How to convert from object to String
Got everything working. Thanks for the help!
- 10-18-2012, 08:25 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
How do I convert a String[] object to a Set?
By RPFeltz in forum New To JavaReplies: 3Last Post: 08-20-2011, 04:15 PM -
Convert object to String
By vinnie in forum New To JavaReplies: 10Last Post: 12-12-2010, 05:45 AM -
Convert object to String
By innspiron in forum New To JavaReplies: 3Last Post: 03-14-2010, 01:26 PM -
How to convert string array into object in java
By kgkamaraj in forum New To JavaReplies: 4Last Post: 02-12-2010, 12:33 PM -
convert a string to an object in java
By jforce93 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 11:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks