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 5 of 5

Thread: Printing From Another Class.

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Printing From Another Class.

    I am trying to print from a different class, and this is my current code. The last code entry is the mainclass, and "Card" is showing up as cannot find symbol error. I am trying to print printCard using variables (1, 11).
    public class CardWork {
     
     
        /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Logan
     */
    public class Card {
        int suit, rank;
     
        public Card () {
            this.suit = 0; this.rank = 0;
        }
     
     
     
        public void printCard (Card c) {
            String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
            String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
    "7", "8", "9", "10", "Jack", "Queen", "King" };
     
        System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
     
    }
     
     
    }
     
    }
    And this is my mainclass
    public class Cards {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Card card = new Card (1, 11);
            printCard (card);
     
        }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Printing From Another Class.

    printCard is not a method of Cards, but the way you are calling it is as if you assume that it was a static method of this class. No, you need to call this method off of a Card instance, here off of the card variable.

    So the method should be called like so:

       card.printCard();

    Note, this method should not take a parameter as it is printing the current card object, not some Card that you pass into it.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing From Another Class.

    Hello Curmudgeon, what if I want to pass (1, 11) into the method printCard? Would I add (1, 11) into the printCard method?

    --- Update ---

    Also, in main I have tried adding card.printCard(); and it is telling me card is not a symbol.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Printing From Another Class.

    Quote Originally Posted by LoganC View Post
    Hello Curmudgeon, what if I want to pass (1, 11) into the method printCard? Would I add (1, 11) into the printCard method?
    This doesn't make sense. The method printCard() should print the current card, and that's it. It shouldn't be used to set fields in a Card object or anything else. It should just print the Card, period.

    [/COLOR]Also, in main I have tried adding card.printCard(); and it is telling me card is not a symbol.
    Because you need to change the method in Card so that it takes no parameters.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing From Another Class.

    I have tried removing the parameters and still I am getting cannot find symbol on card.printCard();
    public class Card {
        int suit, rank;
     
        public Card () {
            this.suit = 0; this.rank = 0;
        }
     
     
     
        public void printCard () {
            String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
            String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
    "7", "8", "9", "10", "Jack", "Queen", "King" };
     
        System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
     
     
     
    }
     
     
    }
     
    }

    public class Cards {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            card.printCard();
     
     
        }
    }


    --- Update ---

    Fixed the issue. Thanks Cur.

Similar Threads

  1. toString not printing everything in the Array which is in a seperate class
    By taz_1891 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2012, 12:51 PM
  2. Calling a print method from another class (printing array)
    By Kaldanis in forum Object Oriented Programming
    Replies: 7
    Last Post: November 25th, 2011, 01:32 PM
  3. Replies: 1
    Last Post: September 28th, 2011, 08:29 AM
  4. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 03:41 AM
  5. Having trouble printing object information in main class
    By KingLane in forum Object Oriented Programming
    Replies: 1
    Last Post: October 11th, 2009, 07:53 PM