Im getting 1 Silly error can someone help please
Code :
import java.util.Arrays;
import java.util.List;
public class PhoneBook(<PhoneBookEntry> directory){
{
int numEntries = Math.max(directory.size(), DEFAULT_SIZE);
this.directory = new PhoneBookEntry[numEntries];
for (PhoneBookEntry entry : directory)
{
this.directory[nextEntryIndex++] = entry;
}
}
public void add(PhoneBookEntry entry)
{
directory[nextEntryIndex++] = entry;
}
public void remove(PhoneBookEntry entry)
{
for (int i = 0; i < nextEntryIndex; ++i)
{
if (entry.equals(directory[i]))
{
directory[i] = null;
}
}
}
public PhoneBookEntry findByName(String name)
{
PhoneBookEntry value = null;
for (int i = 0; i < nextEntryIndex; ++i)
{
if ((directory[i] != null) && (directory[i].getName().equals(name)))
{
value = directory[i];
break;
}
}
return value;
}
public String toString()
{
StringBuilder builder = new StringBuilder(DEFAULT_BUFFER_SIZE);
builder.append("PhoneBook{");
for (int i = 0; i < nextEntryIndex; ++i)
{
PhoneBookEntry entry = directory[i];
if (entry == null)
{
builder.append("null");
}
else
{
builder.append(entry.toString());
}
builder.append(",");
}
builder.append('}');
return builder.toString();
}
}
}
Code :
public class PhoneBook(<PhoneBookEntry> directory){
what am i doing wrong the error says i need to add a { can anyone see wer im goin wrong please?
Re: Im getting 1 Silly error can someone help please
Steve has never taught syntax like this <PhoneBookEntry>, unless it appears after ArrayList. But it has other uses too. Remove < and >
Chris,
Bangor University
Re: Im getting 1 Silly error can someone help please
hey chris cheers for the reply. have removed the < > still gettin the same error dude. havent had much joy with this program its due in tomoz too.. bummer lol
Re: Im getting 1 Silly error can someone help please
You have merged the start of your class ie,
with your constructor
Chris
Re: Im getting 1 Silly error can someone help please
u know what chris its that late in the day i cant think straight bud. ive ammended my code but no such luck. are you studying with steve in bangor dude?
Code :
import java.util.Arrays;
import java.util.List;
public class PhoneBook{
public class PhoneBook(PhoneBookEntry directory)
{
int numEntries = Math.max(directory.size(), DEFAULT_SIZE);
this.directory = new PhoneBookEntry[numEntries];
for (PhoneBookEntry entry : directory)
{
this.directory[nextEntryIndex++] = entry;
}
}
Re: Im getting 1 Silly error can someone help please
Yes I am. Exactly what errors are you getting now, since from what I can see you don't have a great deal wrong with your code.
you don't have a global "directory" defined like you are trying to access on the line this.directory = new Phone....
Paste all your amended code and all of the error messages its hard to properly debug your code without it. I can only give you limited help really.
Might I as who you are?
Re: Im getting 1 Silly error can someone help please
Code :
import java.util.Arrays;
import java.util.List;
public class PhoneBook{
public class PhoneBook(PhoneBookEntry directory)
{
int numEntries = Math.max(directory.size(), DEFAULT_SIZE);
this.directory = new PhoneBookEntry[numEntries];
for (PhoneBookEntry entry : directory)
{
this.directory[nextEntryIndex++] = entry;
}
}
public void add(PhoneBookEntry entry)
{
directory[nextEntryIndex++] = entry;
}
public void remove(PhoneBookEntry entry)
{
for (int i = 0; i < nextEntryIndex; ++i)
{
if (entry.equals(directory[i]))
{
directory[i] = null;
}
}
}
public PhoneBookEntry findByName(String name)
{
PhoneBookEntry value = null;
for (int i = 0; i < nextEntryIndex; ++i)
{
if ((directory[i] != null) && (directory[i].getName().equals(name)))
{
value = directory[i];
break;
}
}
return value;
}
public String toString()
{
StringBuilder builder = new StringBuilder(DEFAULT_BUFFER_SIZE);
builder.append("PhoneBook{");
for (int i = 0; i < nextEntryIndex; ++i)
{
PhoneBookEntry entry = directory[i];
if (entry == null)
{
builder.append("null");
}
else
{
builder.append(entry.toString());
}
builder.append(",");
}
builder.append('}');
return builder.toString();
}
}
}
im getting the same 1 error message as i did in the beginning. Lets just say im a newbie to java currently at bangor lol how have u got on with your assignment, what experience have u had doing java then? would you have time to compile my program just to see first hand?
Re: Im getting 1 Silly error can someone help please
My assignment was completed before the christmas holidays.
Formal experience, well the last semester here at Bangor. Other than that I have taught myself I know a few programming languages including C++.
Your program consists of multiple classes so i woud require more files.
Your constructor has been defined as a class.
Code :
public class PhoneBook(PhoneBookEntry directory)
Re: Im getting 1 Silly error can someone help please
fair do's fella. i dont think im the only one sorts struggling on this assignment. i know a few.. ive done task 1 no probz. hopefully get the next 2 programs out of the way by tomoz.. what program do u want my entry one?
Re: Im getting 1 Silly error can someone help please
Code :
class PhoneBookEntry implements Serializable
{
private String name;
private String number;
public PhoneBookEntry(String name, String number)
{
this.name = name;
this.number = number;
}
public String getName()
{
return name;
}
public String getNumber()
{
return number;
}
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
PhoneBookEntry that = (PhoneBookEntry) o;
if (name != null ? !name.equals(that.name) : that.name != null)
{
return false;
}
if (number != null ? !number.equals(that.number) : that.number != null)
{
return false;
}
return true;
}
public String toString()
{
return new StringBuilder().append("PhoneBookEntry{").append("name='").append(name).append('\'').append(", number='").append(number).append('\'').append('}').toString();
}
}
}
Re: Im getting 1 Silly error can someone help please
if you make the change i suggested of removing the word class from your constructor it should work fine.
I shouldnt need to compile your programs. Also im wondering why your class implements seralizable lol
Re: Im getting 1 Silly error can someone help please
bad advice from another java programmer that i know lol
Code :
import java.util.Arrays;
import java.util.List;
public class PhoneBook
{
public PhoneBook(PhoneBookEntry directory)
{
int numEntries = Math.max(directory.size(), DEFAULT_SIZE);
this.directory = new PhoneBookEntry[numEntries];
for (PhoneBookEntry entry : directory)
{
this.directory[nextEntryIndex++] = entry;
}
}
public void add(PhoneBookEntry entry)
{
directory[nextEntryIndex++] = entry;
}
public void remove(PhoneBookEntry entry)
{
for (int i = 0; i < nextEntryIndex; ++i)
{
if (entry.equals(directory[i]))
{
directory[i] = null;
}
}
}
public PhoneBookEntry findByName(String name)
{
PhoneBookEntry value = null;
for (int i = 0; i < nextEntryIndex; ++i)
{
if ((directory[i] != null) && (directory[i].getName().equals(name)))
{
value = directory[i];
break;
}
}
return value;
}
public String toString()
{
StringBuilder builder = new StringBuilder(DEFAULT_BUFFER_SIZE);
builder.append("PhoneBook{");
for (int i = 0; i < nextEntryIndex; ++i)
{
PhoneBookEntry entry = directory[i];
if (entry == null)
{
builder.append("null");
}
else
{
builder.append(entry.toString());
}
builder.append(",");
}
builder.append('}');
return builder.toString();
}
}
public class PhoneBookEntry
{
private String name;
private String number;
public PhoneBookEntry(String name, String number)
{
this.name = name;
this.number = number;
}
public String getName()
{
return name;
}
public String getNumber()
{
return number;
}
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
PhoneBookEntry that = (PhoneBookEntry) o;
if (name != null ? !name.equals(that.name) : that.name != null)
{
return false;
}
if (number != null ? !number.equals(that.number) : that.number != null)
{
return false;
}
return true;
}
public String toString()
{
return new StringBuilder().append("PhoneBookEntry{").append("name='").append(name).append('\'').append(", number='").append(number).append('\'').append('}').toString();
}
}
}
have tried all i can now but cant get my head around it im gettin 2 errors now for the very end of my program which is "class or interface" expected..i enjoy java like but it frustrates the hell out of me. what sites or books would you recommend dude?>
Re: Im getting 1 Silly error can someone help please
You can't have two public classes in one file it should be noted. Also you have more } than you do {.
The Java API is very helpful. As for what sites and books well, javabat.com is a good site to practise youre work. But there is no good site for teaching Java, nor ca I recommend and books, however there is the ever so popular Deitel & Deitel how to program Java books, which many people are fond of Including Steve, but it requires you ti have a basic understanding of Java. Otherwise indeed Big Java which Steve has suggested is a good book for you. I learnt from experimentation and the Java API :)
Chris
Re: Im getting 1 Silly error can someone help please
Cheers for your input anywayz dude im guna hit the hay pretty soon will try again after my first lecture we got till 5 anywayz so hopefully i will have it sorted by then... will take on board what youve said tho much appreciated
Re: Im getting 1 Silly error can someone help please