Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Lottery Program

  Asked By: Erica    Date: Sep 23    Category: Java    Views: 1676
  

I have been working on this problem for the last few weeks and not
got anywhere at all. What i am trying to do is make a lottery program
that stores name, tel no and 4 lottery numbers to a txt file and then
when the program is run it checks 4 numbers against the ones in the
txt file.

So far i have got a program to work that checks a txt file for the
lowest number when only 1 number is there (not 4) and i have another
program which creates 4 random numbers.

What i have so far is below:


import java.io.*;
import javax.swing.*;

public class Super7 {

private static String[] punter_name = new String [20];
private static String[] tel_number = new String [20];
private static int selection[] = new int[30][4];
private static int linecount = 0;
private static int counter = 0;


public static void main(String[] args){

String fname = JOptionPane.showInputDialog("Enter your file
name/path");

System.exit(0);
}
}

public static void process(String l)
{
if (counter >= 20){
System.out.println("Arrays are full!");
return;
}
if (linecount % 3 == 0) {
punter_name[counter] = i;
}
else if (linecount % 3 == 1) {
phone_number[counter] = i;
}
else {
StringTokenizer st = new StringTokenizer(s);
int j = 0;
while (st.hasMoreTokens()) {
balls [j] = Integer.parseInt (st.NextToken());
j++;
}
}

public static int readFile(String filenm) {
int count = 0;
FileReader fileread;
String line = "xxxx";
try {
fileread = new FileReader (filenm);
}
catch (IOException e) {
errorMessage ("Error unable to open the file");
return 0;
}
BufferedReader br = new BufferedReader (fileread);
while (line != null) {
try {
line = br.readLine();
if (line != null) {
process (line);
count ++;
}
}
catch (IOException e) {
errorMessage("Error unable to read the file");
line = null;
}
}
try {
br.close();
fileread.close();
}
catch (IOException e) {
errorMessage ("Error closing file");
}
return count;
}

public static void errorMessage (String m) {
JOptionPane.showMessageDialog (null, m, "Error",
JOptionPane.ERROR_MESSAGE);
}
}

for (int index=0;index< myLottoArray.length;index++)
{
myLottoArray[index] = (int)(Math.random()*49+1);
}

for(int counter=1;counter<myLottoArray.length;counter++)
{
for(int index=0;index< myLottoArray.length -1;index++)
{
if (myLottoArray[index] > myLottoArray[index + 1])
{
int jumble = myLottoArray[index];
myLottoArray[index]= myLottoArray[index + 1];
myLottoArray[index + 1] = jumble;
}//end if

System.out.println("The Lottery Numbers are:");

for(int index=0;index< myLottoArray.length;index++)
{
System.out.print( + myLottoArray[index] + " ");
}
}

Obviosuly its a long way off working, the code i posted is the two
programs combined. If anyone could help i would be very grateful. Or
if you have some good hints that would be good.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Jonah Brown     Answered On: Sep 23

Where does the data come from? Is it aready stored on
the hard drive as a txt  file or does the program  have
to let the user input it?

Here is a simplified program that reads data from a
txt file  and allows you to compare it to winning
numbers (I didn't write that part yet though). You'll
find the txt file with the customer's data after the
program:

import java.sql.*;
import java.lang.*;

public class  lotteryTest
{
public static  void main(String args[])
{
String[] custNumbers = new String[4];
try
{
//Query the data file (note: txtDsn is a text
data source that points to the directory that contains
lotteryData.txt).
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection1 =
DriverManager.getConnection("jdbc:odbc:txtDsn");
Statement statement1 =
connection1.createStatement();
String sql = "SELECT * FROM lotteryData.txt";

ResultSet rs = statement1.executeQuery(sql );
while( rs.next() )
{
for(int i = 4; i<8;i++)
{
//grab the customer's numbers  into an array
custNumbers[i-4] = rs.getString(i);
System.out.println(custNumbers[i-4]);
}
System.out.println("");
//Now all you have to do is compare the
customer's numbers to the winning numbers
//I haven't done that bit yet though, maybe
//save them in a hashmap, sort it from lowest to
highest and compare them to the winning numbers?
}
statement1.close();
connection1.close();
}
catch (Exception e)
{
System.err.println(e);
}
}
}

Here is the data file (lotteryData.txt):

FirsNm, LastNm, phone, no1, no2, no3, no4,
Fred, Bloggs, 51332555984, 12, 25, 36, 45,
Jane, Bloggs, 51332489512, 10, 15, 29, 48,

 
Answer #2    Answered By: Boyce Fischer     Answered On: Sep 23

I made the code do a bit more, but I still don't know
where the customer data comes from. I've assumed for
now that it comes from a txt  file on the hard drive.

Here's the altered code, however it has a flaw in that
it doesn't ensure that the winning numbers  are all
unique. I'm sure that wouldn't be too hard to fix
though.

import java.sql.*;
import java.lang.*;
import java.util.*;

public class  lotteryTest
{
int[] winningNumbers = new int[4];

public static  void main(String args[])
{
lotteryTest runner = new lotteryTest();

//Connect to the customer data file.
connectionHandler connections = new
connectionHandler();
connections.connect();
Statement statement1 = connections.statement1;

//Get the customer's numbers from the data file
and compare them to the winnning numbers.
runner.getCustNumbers(statement1);

//Close the connection to the data file.
connections.closeConnection();
}

lotteryTest()
{
//Generate the four winning numbers. Note
presently this does Not eliminate the possibility of
same numbers occurring:
for(int u = 0;u<4; u++)
{
double a = Math.random() *100;
winningNumbers[u] = (int)Math.round(a);
}
Arrays.sort(winningNumbers);
}

void getCustNumbers(Statement statement1)
{
try
{
int[] custNumbers = new int[4];
boolean awinner = false;

String sql = "SELECT * FROM lotteryData.txt";
ResultSet rs = statement1.executeQuery(sql );
while(rs.next())
{
String firstNm = rs.getString(1);
String lastNm = rs.getString(2);

//grab the customer's numbers into an array
for(int i = 4; i<8;i++)
custNumbers[i-4] = rs.getInt(i);
Arrays.sort(custNumbers);

/*
//A way to ensure a winner, just for testing
winningNumbers[0] = 10;
winningNumbers[1] = 15;
winningNumbers[2] = 29;
winningNumbers[3] = 48;
*/

//Compare the customer's numbers to the
winning numbers:
if(custNumbers[0] == winningNumbers[0] &&
custNumbers[1] == winningNumbers[1] && custNumbers[2]
== winningNumbers[2] && custNumbers[3] ==
winningNumbers[3])
{
//Show the winning numbers:
for(int o = 1; o<5;o++)
System.out.println("winning number  "+ o +"
is "+winningNumbers[o-1]);

System.out.println("\nThe winner is " +
firstNm +" "+lastNm+"!");
awinner = true;
}
}
if(!awinner)
{
//Show the winning numbers:
for(int o = 1; o<5;o++)
System.out.println("winning number "+ o +" is
"+winningNumbers[o-1]);

System.out.println("\nNo winners this time!");
}
}
catch(Exception en)
{
System.out.println(en);
}
}
}

class connectionHandler
{
Statement statement1;
Connection connection1;

void connect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection1 =
DriverManager.getConnection("jdbc:odbc:txtDsn");
statement1 = connection1.createStatement();
}
catch (Exception e)
{
System.err.println(e);
}
}

void closeConnection()
{
try
{
statement1.close();
connection1.close();
}
catch (Exception ex)
{
System.err.println(ex);
}
}
}

 
Didn't find what you were looking for? Find more on Lottery Program Or get search suggestion and latest updates.




Tagged: