The Artima Developer Community
Sponsored Link

Java Answers Forum
Lotto Number Match

3 replies on 1 page. Most recent reply: May 13, 2004 11:39 AM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
George McClenton

Posts: 1
Nickname: apogee
Registered: May, 2004

Lotto Number Match Posted: May 6, 2004 9:03 AM
Reply to this message Reply
Advertisement
I'm new to java programming, and went through a couple different books, doing examples and challenge problems. But now I'm trying to generate a program on my own to see if I understand the language and it's not going well. Essentially I want to have a program that will ask a user to input the lotto numbers and then open a file of tickets up to read those tickets' numbers line by line and compare them against the given input. I figure I can just do it as strings, ensuring that all tickets will be arranged from low to high. So, I just have to open the file, read the file, and then make each line into a string and compare it.


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Lotto Number Match Posted: May 6, 2004 6:36 PM
Reply to this message Reply
And your question being?

SRV001

Posts: 13
Nickname: srv001
Registered: May, 2004

Re: Lotto Number Match Posted: May 13, 2004 6:39 AM
Reply to this message Reply
/**
 Ask user to input the lotto numbers (7)
 Open a file of tickets
 Read ticket numbers line by line and compare them against the given input.
 
 Enter seven numbers per line in a file called myNumbers.txt
 Make sure that there is no lines (crlf) or spaces after the
 last number in the file.
 ex.
 01 02 03 04 05 06 07
 01 03 05 07 09 11 13
 22 25 31 37 41 43 47
*/
 
import java.io.*;
import java.util.*;
 
public class LottoChecker {
 
    private static final int MAXNUM = 7;
    private static final String FNAME = "myNumbers.txt";
 
    private boolean winner;
    private int[] numbers;
 
   /**
    Construct a LottoChecker object that will read the winning numbers
    from the console and check them against a file of tickets.
    */
    public LottoChecker() {
        this.winner = false;
        this.numbers = new int[MAXNUM];
    }
 
   /**
    getResult
    @return result
    */
    public boolean getResult() {
        return this.winner;
    }
 
   /**
    Prompt the user to Enter the winning Lotto numbers to be checked
    against the file of tickets
    */
    public void inputNumbers() {
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader console = new BufferedReader(reader);
        String input = null;
 
        System.out.print("Please enter the winning Lotto numbers: ");
 
        try {
            input = console.readLine();
 
            int j = 0;
            StringTokenizer tokenizer = new StringTokenizer(input);
            while ( tokenizer.hasMoreTokens() ) {
                numbers[j++] = Integer.parseInt(tokenizer.nextToken());
            }
        } catch (Exception e) {
            System.out.println("Error entering Lotto numbers");
            System.exit(0);
        } finally {
            System.out.println("\nYou Entered Numbers: " + input + "\n");
        }
    } //inputNumbers
 
   /**
    Check the input numbers against the numbers stored in file
    Match each number to win
    */
    public void checkNumbers() {
        try {
            FileInputStream fis = new FileInputStream(FNAME);
            InputStreamReader ir = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(ir);
 
            int recCounter = 0;
            boolean winTicket = false;
 
            String s = br.readLine();
            while (s != null) {
                int k = 0;
                int[] myNumbers = new int[MAXNUM];
 
                StringTokenizer tokenizer = new StringTokenizer(s);
                while ( tokenizer.hasMoreTokens() ) {
                    myNumbers[k++] = Integer.parseInt(tokenizer.nextToken());
                }
 
                int l = 0;
                for (int j = 0; (j < numbers.length) && (!winner); j++) {
                    if (myNumbers[l] ==  numbers[j]) {
                        if  (!winner) l++;
                        if (l == myNumbers.length) {
                            winner = true;
                            winTicket = true;
                        }
                    } else if (l > 0) l = 0;
                }
 
                System.out.print(++recCounter + ") Number: " + s.substring(1, 20));
 
                if (winTicket) {
                    System.out.println(" <---");
                    winTicket = false;
                } else {
                    System.out.println();
                }
 
                s = br.readLine();
            }
            br.close();
        } catch(Exception e) {
            System.out.println(e.getMessage());
            System.exit(0);
        }  //try
    } //checkNumbers
 
   /**
    Main program
    Test the LottoChecker program
    */
    public static void main(String[] args) {
        LottoChecker lotto = new LottoChecker();
 
        lotto.inputNumbers();
        lotto.checkNumbers();
 
        if (lotto.getResult() == true) {
            System.out.println("\n-------------------------");
            System.out.println("You have won the lottery!");
            System.out.println("-------------------------\n");
        } else {
            System.out.println("\n---------------------------------");
            System.out.println("Better luck next time (You Lost)!");
            System.out.println("---------------------------------\n");
        }
    } //main()
 
} //LottoChecker

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Lotto Number Match Posted: May 13, 2004 11:39 AM
Reply to this message Reply
SRV001,

I realize that you mean well, but you aren't really helping these people. It is one thing to point out errors, and suggest strategies, but if you do all of their work for them, how are they to learn?

Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime.

Flat View: This topic has 3 replies on 1 page
Topic: Type Casting Previous Topic   Next Topic Topic: A java based Game

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use