Results 1 to 17 of 17
  1. #1
    lieni is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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

  3. #3
    turanan is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    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;
    }

  4. #4
    lieni is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    thank you for your suggestions!:D

  5. #5
    lieni is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

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

  6. #6
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    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

  7. #7
    lieni is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    but the compiler does not stop there it stops line after where there is the return.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Show your code with code tags, the exact error message, and indicate with comments the line that causes the error to occur.

    Luck.

  9. #9
    lieni is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    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?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by lieni View Post
    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.

  11. #11
    lieni is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    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.

  12. #12
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    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.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    What is 'c'?
    That error is a compiler error isn't it? In which case 'c' is presumably a Contact and not a contact[].

  14. #14
    lieni is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    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?

  15. #15
    lieni is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    10q tolls that solved my problem :D and collin thanks for pointing out your suggestion:D

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by collin389 View Post
    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.
    This is incorrect: arrays are Serializable if their element types are and an ObjectOutputStream can write entire arrays in one sweep.

    kind regards,

    Jos

  17. #17
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

Similar Threads

  1. Java problem help!
    By wexgal in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 01:52 PM
  2. java problem
    By Mj Shine in forum New To Java
    Replies: 5
    Last Post: 08-15-2009, 06:09 AM
  3. Java Problem. Need Help!
    By bob101 in forum New To Java
    Replies: 6
    Last Post: 03-19-2009, 05:34 AM
  4. Help with variable assigment to String
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:43 AM
  5. java SE 6 problem
    By techlance in forum Java Applets
    Replies: 1
    Last Post: 06-28-2007, 11:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •