473,394 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help printing Array

8
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.
Expand|Select|Wrap|Line Numbers
  1. public class Ticket
  2. {
  3.    int theticket[]; // token to be inserted on the array
  4.  
  5.  
  6.    Ticket(int a[])
  7.    {
  8.       theticket = new int[6];
  9.  
  10.    }
  11. }
  12.  
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(therefore 6 * 30 numbers) from ArrayTicket Class.
Any idea, how can do it?
Dec 5 '07 #1
8 1550
dmjpro
2,476 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. public class Ticket
  2. {
  3.    int theticket[]; // token to be inserted on the array
  4.  
  5.  
  6.    Ticket(int a[])
  7.    {
  8.       theticket = new int[6];
  9.  
  10.    }
  11. }
  12.  
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(therefore 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
Dec 5 '07 #2
virkof
8
I cannot used two dimensional arrays... specifications of the assignment. Thanks anyway.
Dec 5 '07 #3
dmjpro
2,476 2GB
I cannot used two dimensional arrays... specifications of the assignment. Thanks anyway.
Simply place linearly.
Then do something like this.

Expand|Select|Wrap|Line Numbers
  1. for(int i=0;<30;i++)
  2. {
  3.         for(int j=0;j<6;j++) System.out.println(array[i*6+j]);
  4.         System.out.println("\n"); //Windows Specific
  5. }
  6.  
Debasis Jana
Dec 5 '07 #4
virkof
8
Simply place linearly.
What do you mean by that?
Dec 5 '07 #5
JosAH
11,448 Expert 8TB
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(therefore 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:

Expand|Select|Wrap|Line Numbers
  1. for (int i= 0; i < 30; i++)
  2.    System.out.println(ticket[i]);
  3.  
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
Dec 5 '07 #6
virkof
8

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?
Expand|Select|Wrap|Line Numbers
  1. 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!
Dec 5 '07 #7
r035198x
13,262 8TB
If I use a toString() what should I be returning?
Expand|Select|Wrap|Line Numbers
  1. 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.
Dec 5 '07 #8
JosAH
11,448 Expert 8TB
If I use a toString() what should I be returning?
Expand|Select|Wrap|Line Numbers
  1. 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
Dec 5 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been...
5
by: Chris Schumacher | last post by:
I'm working on a program which splits a long string into a series of smaller ones. It is supposed to work as follows; 1. You enter a string into variable "str" 2. The program searches for the...
16
by: Simon | last post by:
I am a programming student and have recently missed two weeks of school due to a serious injury in a car accident. I am completing all of my assignments (via Labs) but have come across the...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
1
by: mark | last post by:
I can't seem to get the logic for printing multiple pages. I know I have to do a comparison between the bottom margin and the position of the next line to be printed and initiate a has more pages...
6
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
10
by: preethamkumark | last post by:
- The program first creates a shared memory buffer containing an array of 20 integers. - Each slot of the buffer can have either 0 or 1, where 0 represents an empty slot, and 1 represents an...
3
by: deepan | last post by:
Hai everybody! I've recently joined this group.Really amazed at the activity of this group! Please somebody could suggest me a c code to search for the no. in a given array of no.s without...
2
by: Latina | last post by:
Hi, Can some one help to figure out why is only printing '{ }' and not the values the user is entering? Here is part of my code: void IntegerSet::setString() { cout<<"{"; for(element=0;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.