Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Need help!!

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help!!

    Haiii everyone.. i really need help about this code.. please someone help me..

    public class Ant 
    {
        private int Row = 0;
        private int Column = 0;
        private String [] [] ArrayAnt;
     
        public Ant(int r, int c)
        {
            Row = r;
            Column = c;
            ArrayAnt = new String[Row] [Column];
            Grid();
        }
     
        private void Grid()
        {
            for(int r = 0; r < Row; r++)
            {
                for(int c = 0; c < Column; c++)
                {
                    ArrayAnt[r][c] = "W";
                }
            }
        }
    }
     
     
     
    public class ProgramAnt1 
    {
         public static void main(String[] args) 
         {
             Ant ant = new Ant(3,3);
             System.out.println(ant);
         }
    }


    that code cannot print the array.. please help me to solve it.. thanks..
    Last edited by helo; July 6th, 2011 at 12:46 PM.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help!!

    It helps to be a lot more specific than that. I can assume you want to print the variable ArrayAnt (for what its worth, I'd recommend reading up on java code conventions)? Then print the array or loop over the array and print its values (as opposed to printing the Object, which only calls the toString() method of said object - see the API for object to determine what this (by default) is)

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Need help!!

    If you really want to print Ant by passing it to a println method, you must write an Ant.toString() method that returns a String containing the Ant details you want printed.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help!!

    Quote Originally Posted by copeg View Post
    It helps to be a lot more specific than that. I can assume you want to print the variable ArrayAnt (for what its worth, I'd recommend reading up on java code conventions)? Then print the array or loop over the array and print its values (as opposed to printing the Object, which only calls the toString() method of said object - see the API for object to determine what this (by default) is)
    yes thanks for the information... coz in my assignment they said cannot using toString() method.. anyway thanks

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help!!

    Quote Originally Posted by dlorde View Post
    If you really want to print Ant by passing it to a println method, you must write an Ant.toString() method that returns a String containing the Ant details you want printed.
    thanks for information.. anyway the tester is given by the assignment like that (System.out.println(ant)..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help!!

    System.out.println(ant);
    If that is the statement that you must use to print out the contents of an Ant instance and you are not allowed to add a toString() method to your class, then the output from that println will be from the default Object class's toString method which will look like: Ant@123dd24

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help!!

    here u are..


    class Ant{
    //edited by moderator
    }
    Last edited by copeg; July 26th, 2011 at 04:18 PM.

  8. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help!!

    Quote Originally Posted by iknowthatforgo View Post
    here u are..
    thanks for it.. haha.. does any one show me how to use toString method in this code? just for studying..
    Last edited by copeg; July 26th, 2011 at 04:19 PM.

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help!!

    iknowthatforgo, please read the following.
    http://www.javaprogrammingforums.com...n-feeding.html

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help!!

    I assume you are asking how to define a class's toString() method.
    Read the API doc for Object class's toString method.
    For the Ant class, say you want to see some of its instance variables.
    public String toString() {
       return "Ant: row=" + row + ", column=" + column;  // return our variables
    } // end toString

  11. #11
    Member
    Join Date
    Jul 2011
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help!!

    System.out.println does not understand class ant. How about adding a func called printAnt() to the class. Inside the method have row and col loops as you had to load it as in, but instead of assigning a char you print it. Between loops, remember to println.