Help printing Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • virkof
    New Member
    • Nov 2007
    • 8

    #1

    Help printing Array

    Hi everyone... I am very stuck here trying to print from an Array. Can somebody help please?

    Basically I have two different classes, one, which creates Tickets Objects. Each object is an array of 6.
    Code:
    public class Ticket
    {
       int theticket[]; // token to be inserted on the array
       
       
       Ticket(int a[])
       {
          theticket = new int[6];
           
       }
    }
    The second Class, called ArrayTicket is an array of 30 Tickets. Is in this class, where I assign 6 number to each Ticket. My problems is:
    I need to print the 30 Tickets(therefo re 6 * 30 numbers) from ArrayTicket Class.
    Any idea, how can do it?
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by virkof
    Hi everyone... I am very stuck here trying to print from an Array. Can somebody help please?

    Basically I have two different classes, one, which creates Tickets Objects. Each object is an array of 6.
    Code:
    public class Ticket
    {
       int theticket[]; // token to be inserted on the array
       
       
       Ticket(int a[])
       {
          theticket = new int[6];
           
       }
    }
    The second Class, called ArrayTicket is an array of 30 Tickets. Is in this class, where I assign 6 number to each Ticket. My problems is:
    I need to print the 30 Tickets(therefo re 6 * 30 numbers) from ArrayTicket Class.
    Any idea, how can do it?
    Simply take a two dimensional Array.
    Each row indicates a Ticket containing 6 numbers.
    Then print them using twi for loops.
    Now try some code and paste here .... The experts are here to correct your code.

    Debasis Jana

    Comment

    • virkof
      New Member
      • Nov 2007
      • 8

      #3
      I cannot used two dimensional arrays... specifications of the assignment. Thanks anyway.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by virkof
        I cannot used two dimensional arrays... specifications of the assignment. Thanks anyway.
        Simply place linearly.
        Then do something like this.

        [code=java]
        for(int i=0;<30;i++)
        {
        for(int j=0;j<6;j++) System.out.prin tln(array[i*6+j]);
        System.out.prin tln("\n"); //Windows Specific
        }
        [/code]

        Debasis Jana

        Comment

        • virkof
          New Member
          • Nov 2007
          • 8

          #5
          Originally posted by dmjpro
          Simply place linearly.
          What do you mean by that?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by virkof
            The second Class, called ArrayTicket is an array of 30 Tickets. Is in this class, where I assign 6 number to each Ticket. My problems is:
            I need to print the 30 Tickets(therefo re 6 * 30 numbers) from ArrayTicket Class.
            Any idea, how can do it?
            Think objects; your other class just wants to print 30 tickets, so it prints them:

            [code=java]
            for (int i= 0; i < 30; i++)
            System.out.prin tln(ticket[i]);
            [/code]

            All that is needed here is a 'toString()' method in the Ticket class itself: a Ticket
            should be able to cough up a String representation of itself. If it has to 'print' six
            numbers, so be it: have a look at the StringWriter class. It can be of great help.

            kind regards,

            Jos

            Comment

            • virkof
              New Member
              • Nov 2007
              • 8

              #7
              Originally posted by JosAH

              All that is needed here is a 'toString()' method in the Ticket class itself: a Ticket
              should be able to cough up a String representation of itself. If it has to 'print' six
              numbers, so be it: have a look at the StringWriter class.
              If I use a toString() what should I be returning?
              Code:
              ticket[i]
              Another of my problems is that when I try to call one of the methods from the main. A Compilation error comes up saying that a non-static method can not be referred from a static context or something like that.

              this is very confusing!

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by virkof
                If I use a toString() what should I be returning?
                Code:
                ticket[i]
                Another of my problems is that when I try to call one of the methods from the main. A Compilation error comes up saying that a non-static method can not be referred from a static context or something like that.

                this is very confusing!
                It shouldn't be confusing at all.
                The error message eplains it nicely. Your main method is static(as are all other typical main methods). Yet you are trying to refer to a non-static method inside it. That is not allowed and the compiler has rightfully told you that. As Jos said above, *think objects*. Thinking objects is the way to go in Java.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by virkof
                  If I use a toString() what should I be returning?
                  Code:
                  ticket[i]
                  Erm, a String? Please read the API documentation of the Object class; it's the
                  mother of all classes and all classes are supposed to have an (inherited) method
                  named toString(). It is supposed to return a String representation of the object.

                  That method is implicitly called by the PrintStream when it should print a non-
                  primitive thing and also by the StringBuilders that are implicitly called by the
                  compiler when it fiddles with the overloaded '+' operator. It's very convenient and
                  most people (such as you) don't even think about it.

                  kind regards,

                  Jos

                  Comment

                  Working...