Results 1 to 4 of 4
  1. #1
    matin1234 is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default overriding toString method

    Hi
    this is my code but when I run it with netbeans it will show just this line instead of my array!!
    the line:run:
    BUILD SUCCESSFUL (total time: 0 seconds)

    and my class is this:


    Java Code:
    public class Score {
    
        public final static int maxSize = 10;
        protected int size;
        protected GameEntry[] entries;
    
        public Score() {
            size = 0;
            entries = new GameEntry[maxSize];
    
    
        }
        public void add(GameEntry e,int b){
    
               entries[b] = e;
    
    
           }
    
    
        @Override
        public String toString() {
            String s = "[";
            for (int i = 0; i < size; i++) {
    
                if (i > 0) {
                    s += entries[i];
                }
                s += ",";
            }
            return s;
    
        }
    
    
        public static void main(String[] args){
            Score x = new Score();
            GameEntry v = new GameEntry("neda", 12);
            GameEntry g = new GameEntry("nima", 18);
            x.add(v, 0);
            x.add(g, 1);
            x.toString();
    
    
    
    
        }
    }

    thanks!!

    Mod Edit: code tags added
    Last edited by Fubarable; 06-01-2010 at 04:17 AM. Reason: mod edit: code tags added

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

    Default

    I hope you don't mind that I added code tags to your post. To learn to do this yourself, please see the link in my signature below.

    anyway to show your String, how about having
    Java Code:
    System.out.println(x);
    as the last line of your main method so that the String created by x is displayed.

  3. #3
    matin1234 is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    thanks!! but it just return : "[" !!!!!!!

  4. #4
    matin1234 is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    Sorry,I get my answer I should write "size++" thanks anyway

Similar Threads

  1. Overriding equals method
    By sky in forum New To Java
    Replies: 7
    Last Post: 03-12-2010, 03:39 PM
  2. Method Overriding - Seriously confused :-(
    By fullmetaljacket in forum New To Java
    Replies: 4
    Last Post: 05-26-2009, 04:18 PM
  3. [SOLVED] Method Overriding
    By MithunDhar in forum New To Java
    Replies: 3
    Last Post: 04-06-2009, 08:02 AM
  4. Replies: 1
    Last Post: 04-01-2009, 02:00 AM
  5. is overriding static method possible
    By raghu in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 12:38 AM

Tags for this Thread

Posting Permissions

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