It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

Problem With ArrayList wrong number printing

Problem With ArrayList wrong number printing

Postby linkinpark69 on Sat Feb 24, 2007 1:33 pm

hi im trying to create a lotto generator which prints and generates a user specific number of lines of 6 sorted random numbers which have to be displayed correctly. to do this i have two classes, one with an array of the 6 randomly sorted generated numbers and the other with an arraylist and iterator

The code for the Numbers class is:

public class Numbers
{
private final int SIZE = 6;
public int numbers[];

public Numbers()
{
numbers = new int [SIZE];
genNumbers();
sort();
}

private void genNumbers()
{
for (int i = 0; i<SIZE; i++)
{
numbers[i]= 1 + (int)(Math.random() * 48 );
if (i>0)
{
for(int j=0; j<i; j++)
{
if (numbers[j] == numbers [i])
{
numbers[j] = 1 + (int)(Math.random() * 48 );
}
}
}
}
}

private void sort()
{
int t=0;
for (int j=0; j<SIZE; j++)
{
for (int i=1; i<SIZE; i++)
{
if (numbers[i-1]>numbers[i])
{
t = numbers[i];
numbers[i] = numbers [i-1];
numbers[i-1] = t;
}
}
}
}

public void printTicket()
{
for (int i =0; i < SIZE; i++)
{
if (numbers[i]<10)
{
System.out.print(" " + numbers [i]);
}
else
{
System.out.print(" " + numbers[i]);
}
System.out.println("");
genNumbers();
sort();
}
}
}


The Code For the Ticket Class is:

import java.util.ArrayList;
import java.util.Iterator;

public class Ticket
{

private int numberOfLines;
private ArrayList lottoLines;

public Ticket(int num)
{
numberOfLines = num;
lottoLines = new ArrayList(numberOfLines);
for(int i = 0; i < numberOfLines; i++)
{
lottoLines.add(new Numbers());
}
}

public void printTicket()
{
System.out.println("**************************************");
System.out.println("** **");
System.out.println("** Lotto Ticket **");
System.out.println("** **");
System.out.println("**************************************");
System.out.println("** **");

Iterator it = lottoLines.iterator();
while (it.hasNext())
{
System.out.print("** ");
for(int i = 0; i < numberOfLines; i++)
{

System.out.print(lottoLines.get(i));
}
System.out.print(" **");
System.out.println();
it.next();
}

System.out.println("** **");
System.out.println("**************************************");
}
}

the problem is the reencing to the elementData but i need help

the current printed display for 3 lines of numbers is:

**************************************
** **
** Lotto Ticket **
** **
**************************************
** **
** Numbers@e06940Numbers@11e0c13Numbers@1aae94f **
** Numbers@e06940Numbers@11e0c13Numbers@1aae94f **
** Numbers@e06940Numbers@11e0c13Numbers@1aae94f **
** **
**************************************

which is incorrect as i want the numbers that are generated and place in the array list printed

any help would be great thank you in advance
linkinpark69
 
Posts: 5
Joined: Sat Feb 24, 2007 1:20 pm

Postby mwa103 on Mon Feb 26, 2007 4:13 pm

Your problem is with the line: System.out.print(lottoLines.get(i));
in the printTicket method (Ticket Class).

You are asking Java to print the results of lottoLines.get(), lottoLines is an arraylist of Numbers. A Number object is an array of ints. Therefor, Java has no idea how it is to be printed so it just prints the memmory address of the object instead of the elements.

All you need to do to remedy this is to call your printTicket() method instead so the line will look like:
lottoLines.get(i).printTicket();

I believe this will fix your problem, if not post back here with your main() class as well so I can see how you are calling these classes.

-Mike

P.S. Horray! Post number 100! Do I get a party? :D
mwa103
100+ Club
 
Posts: 206
Joined: Mon Mar 07, 2005 10:55 pm

Postby linkinpark69 on Mon Feb 26, 2007 7:50 pm

unfortunately that didn't work as it says can not find the method printTicket() there are only the two classes one which generates the numbers and the other which prints the ticket to the output screen i could send you the files via msn if you wish. thank you for replying
linkinpark69
 
Posts: 5
Joined: Sat Feb 24, 2007 1:20 pm

Postby mwa103 on Mon Feb 26, 2007 7:55 pm

Ok, you may have to do a type cast to let Java know what it's dealing with.
Try this:
Numbers tempNums = (Numbers) lottoLines.get(i);
tempNums.printTicket();

**Edit**
That will replace: lottoLines.get(i).printTicket()

Also, looking through your code I noticed what I think is a typo in your printTicket method (Numbers class). I think you should move "System.out.println("");" outside the for loop. The way it is now it will print a new line after each number (if that is what you want, you can just do System.out.println()
mwa103
100+ Club
 
Posts: 206
Joined: Mon Mar 07, 2005 10:55 pm

Thank you!!!

Postby linkinpark69 on Mon Feb 26, 2007 8:03 pm

Thank you very much it now actually prints the numbers at last :D now ive just got to work on the arragement of the printing thanks for the help mike I appreciate it.
linkinpark69
 
Posts: 5
Joined: Sat Feb 24, 2007 1:20 pm

Postby mwa103 on Mon Feb 26, 2007 8:04 pm

No problem. If you have any other issues just post back and I'll try to help you out.
mwa103
100+ Club
 
Posts: 206
Joined: Mon Mar 07, 2005 10:55 pm

Postby linkinpark69 on Mon Feb 26, 2007 8:04 pm

ok i will thanks again :)
linkinpark69
 
Posts: 5
Joined: Sat Feb 24, 2007 1:20 pm

hmm

Postby linkinpark69 on Mon Feb 26, 2007 9:24 pm

Im sorry i thought that was it but what that does is call the printticket method from the numbers class but by doing this it generates new numbers what i am trying to achieve is to print the numbers that are stored in the array list in this line

lottoLines = new ArrayList(numberOfLines);
for(int i = 0; i < numberOfLines; i++)
{
lottoLines.add(new Numbers());
}

so i have already generated the numbers and stored them in an array of numbers in an arraylist but now i need to print out the array of numbers that are stored in the arraylist and i was wondering how to do this?

[EDIT] nevermind lol i solved it i removed the genNumbers(); from the print ticket in the numbers class[/EDIT}
linkinpark69
 
Posts: 5
Joined: Sat Feb 24, 2007 1:20 pm


Who is online

Users browsing this forum: No registered users and 1 guest