Results 1 to 17 of 17
Thread: my java assigment problem!
- 01-22-2010, 10:42 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
my java assigment problem!
Hi ! I have a Java assigment which consists of making a program for a telephone directory. I have this piece of program for which in the line where there is this.Surname it is giving me an error which is saying unreachable statement. Here is the program:
public boolean equals(Contact other)
{
return(other.Name.equals(this.Name));
return(other.Surname.equals(this.Surname));
return(other.ID.equals(this.ID));
return(other.Tel == this.Tel);
return(other.Town.equals(this.Town));
}
can you help me please by sending me an email on lieni_elaine@hotmail.com?
Thank you.
- 01-22-2010, 10:57 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You need to revise your notes on the return keyword to understand what it does. That should let you know why the compiler finds it strange that you have some code after your return.
Also read
Overriding the equals and hashCode methods - Java insights
- 01-22-2010, 11:56 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 19
- Rep Power
- 0
Just use this piece instead
Java Code:public boolean equals(Contact other) { if(other.Name.equals(this.Name) && (other.Surname.equals(this.Surname) && (other.ID.equals(this.ID) && (other.Tel == this.Tel) && (other.Town.equals(this.Town)) { return true; } return false; }
- 01-22-2010, 05:51 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
thank you for your suggestions!:D
- 01-22-2010, 09:12 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
Hey ! thanks again for the suggestions it worked! but now i have another problem(apart from million of them in this assigment:rolleyes:) now the error is: cannot find symbol method compareto(java.lang.String);(btw the iehor is instead of other). Please can you help me once again? Thank you. This is the code:
public int compareto(Contact iehor){
return this.surname.compareto(Contact.this .surname);
}
- 01-22-2010, 11:57 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
I think the class needs to implement Comparable interface, and the method is named compareTo() not compareto(), check out the Comparable interface in the API
- 01-23-2010, 03:49 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
but the compiler does not stop there it stops line after where there is the return.
-
Show your code with code tags, the exact error message, and indicate with comments the line that causes the error to occur.
Luck.
- 01-23-2010, 04:22 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
public int compareto(Contact iehor){
return this.surname.compareto(Contact.this .surname); //here it is giving me an error
}
the error is: cannot find symbol-method compareto(java.lang.String)
is it more clear?
-
You're still missing the code tags here making it harder to read your code. Please read my signature below for more on this.
Other problems here:
1) Java is case sensitive and so compareto is not the same as compareTo. Check out the Java API to see which to use.
2) Your compareTo method gets a Contact object, iehor, and yet your method body completely ignores this parameter. Myself, I would compare the current object's surname (this.surname) with that of the parameter's.
- 01-27-2010, 03:26 PM #11
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
hi I have other problem... i have this piece of problem for which the error is incompatible types-found Contact[] but expected Contact... this is the code:
File inputFile = new File ("agenda.dat");
FileInputStream downStream = new FileInputStream(inputFile);
ObjectInputStream inObjectStream = new ObjectInputStream(downStream);
pos = Utilities.search(conId,myContact);
Contact mycontact[]= null;
//to load whole arraylist
try{
c = (Contact[]) inObjectStream.readObject(); // THIS IS WHERE THE ERROR IS.
}
catch (Exception e)
{System.out.println("\nSomething went wrong when loading file: "+e.getMessage());}//end catch
int n = Contact.size;
System.out.println("Stored contacts\' Details include:\n");
for (int i = 0 ; i <n; i++){
System.out.println("Name: "+ Contact[i].getName());
System.out.println("Surname: "+ Contact[i].getSurname());
System.out.println("Town: " +Contact[i].getTown());
System.out.println("ID: " +Contact[i].getId());
System.out.println("Telephone number: " +Contact[i].getTel());
System.out.println();
}//end for
inObjectStream.close();
break;
Can you help me pls?
Thank you for the support.
- 01-27-2010, 03:59 PM #12
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Hi, most likely, readObject() will only read one object at a time, not a whole array of them, Also, i don't think you can write a whole array becuase you have to be writing a serializable Object, and an array is not serializable.
- 01-27-2010, 04:11 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
What is 'c'?
That error is a compiler error isn't it? In which case 'c' is presumably a Contact and not a contact[].
- 01-27-2010, 06:23 PM #14
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
we are using an arraylist not an array . so it is serializable. c is declared as Contact c = null; in the main program. this piece of program is located in the main program as a case for which it reads entries from a file. what do you think that particular line will become?
- 01-27-2010, 06:35 PM #15
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
10q tolls that solved my problem :D and collin thanks for pointing out your suggestion:D
- 01-27-2010, 06:53 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
- 01-28-2010, 12:06 AM #17
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Similar Threads
-
Java problem help!
By wexgal in forum New To JavaReplies: 3Last Post: 10-26-2009, 12:52 PM -
java problem
By Mj Shine in forum New To JavaReplies: 5Last Post: 08-15-2009, 05:09 AM -
Java Problem. Need Help!
By bob101 in forum New To JavaReplies: 6Last Post: 03-19-2009, 04:34 AM -
Help with variable assigment to String
By silvia in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:43 AM -
java SE 6 problem
By techlance in forum Java AppletsReplies: 1Last Post: 06-28-2007, 10:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks