JAVA Question

Discussion in 'OT Technology' started by XtremelyCanadian, Feb 28, 2003.

  1. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    I have been asked by a company that interviewed me to create a random number generator for a lottery type system. They want me to ask the user how many tickets they want, and from there i create 6 random numbers between 0-49 for each ticket. Seems simple enough, what i want to do is pass that number to a servlet, create an array, then loop through that array in a .jsp page, for each 6 numbers i would create a new ticket. The only problem i have is passing an array from the servlet to the jsp page, i have no idea how to do this. Any help in doing this or a point in the right direction would be greatly appreciated.
     
  2. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    i think you are looking at it the wrong way. (the array area).

    what i would do is keep all my code in the bean, makes it easier to manage and debug.

    2nd i would do something like this:
    Code:
    1st page: number of tickets
     use the get parameters and assign it to a string, lets say a.
     (use  ticket.setNumtickets(a)) that sets ur number of tickets.)
    2nd page: give out ticket numbers
     u let the user know what numbers they have.
     (use ticket.getNumtickets() in a loop starting from 0 though a (being number of tickets)
     have a submit button to the next page, the draw page.
    3rd page: give out the winning numbers
     u let the nuser know the winning numbers
     (use ticket.getWinningnum() in a loop to say the 6 winning numbers (0-5).
     i would also compare the numbers here, maybe use red color to show matching numbers in the tickets?
    
    if u need anymore advice, im home at 6pm pst tonight, and i will have my computer there to help ya. (unless i go home and get mine cause im bored as fuck at work).

    gl on the new job! i plan to get out of this dead end tech repair job onto servlet programming. i hated java when i first started it, but now i love it. like alcohol. :big grin:

    oh i hope that steers u into the right direction :big grin:
     
  3. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    Thanks for the good advice, its always interesting to hear anothers persons approach to problems. Im in the process of coding it right now so i will let you know how it goes, thanks again!!!
     
  4. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    alrite so here goes, its probably a really complicated way of doing this but it is the way i was taught at school.
    JSP CODE(TicketForm.jsp)

    <html>
    <head>
    <title>Lotto 649</title>
    </head>
    <body background = 'goldenrod'>
    <form method = 'post' action = '<%= request.getContextPath()%>/servlet/TicketServlet'>
    <table align='center'>
    <tr><td colspan='2'><h1>Lotto 649</h1></td></tr>
    <tr><td colspan='2'><strong>Please enter the number of tickets you would like...</strong>
    <tr><td>Number of Tickets: </td><td><input type="text" name="TicketNumber"></td></tr>
    <tr><td></td><td><input type="submit" value="Submit" name="submit"><input type="reset" value="Reset" name="reset"></td></tr>
    </form>
    </body>
    </html>


    SERVLET CODE(TicketServlet.java) : jsp is posted to the servlet, which sets the ticket number and calls the RandomNumber function


    import java.sql.*;
    import java.io.*;
    import java.util.*;
    import Matrix.imports.util.*;

    import javax.servlet.*;
    import javax.servlet.http.*;

    public class TicketServlet extends HttpServlet
    {

    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
    {

    try
    {
    Logger.log("in the servlet");
    TicketClassSet ticket = null;

    TicketClass aTicket = new TicketClass();
    HttpSession session = req.getSession(true);
    String sTicketNumber = req.getParameter("TicketNumber");
    int nTicketNumber = Integer.parseInt(sTicketNumber);
    aTicket.setticketNumber(nTicketNumber);
    Logger.log("set the number to the class = " + aTicket.getticketNumber());
    ticket.RandomNumber(aTicket);
    boolean Success = true;
    Logger.log("after the call");
    if (Success == true)
    {
    Logger.log("success");
    res.sendRedirect("<%req.getContextPath()%>/TicketConfirmation.jsp");
    }
    else
    {
    Logger.log("fail");
    res.sendRedirect("<%req.getContextPath()%>/TicketError.jsp");
    }
    }
    catch (SQLException sqle)
    {
    Logger.log(sqle.getMessage());
    }
    catch(ArrayIndexOutOfBoundsException aioobe)
    {
    Logger.log(aioobe.getMessage());
    }
    }
    }

    CLASSES (TicketClassSet.java): first one has the method in it for a random number which im not done yet

    import Matrix.imports.util.*;
    import java.util.*;
    import java.sql.*;

    public class TicketClassSet
    {
    /**
    * The DBSkillsSet class represents a group of messages from the Skills table
    * @author Arabicon
    * @version 1.0, Nov 2002
    */
    public TicketClassSet () throws SQLException
    {

    }

    public boolean RandomNumber(TicketClass aTicket) throws SQLException
    {
    Logger.log("in the random number");
    /**
    * Returns random numbers based on the users input
    * @param
    * @return all random numbers
    */
    Random rand = new Random();

    // Random integers
    int nTicketCount = 0;
    int nNumberCount = 0;
    TicketClass aTicket;
    int TicketNumber = aTicket.getticketNumber();
    while(TicketNumber > nTicketCount)
    {
    int i = rand.nextInt(50);
    }
    return true;
    }
    }

    (TicketClass.java) second one has my get and set methods

    public class TicketClass
    {

    //Member Variables
    private int ticketNumber;

    //default constructor
    public TicketClass()
    {
    ticketNumber = 0;
    }


    //get functions
    public int getticketNumber()
    {
    return ticketNumber;
    }

    //set functions
    public void setticketNumber(int param)
    {
    ticketNumber = param;
    }
    }

    right now its giving me a internal server error of
    java.lang.NullPointerException
    on the line that is in bold type....
    any suggestions ?????
     
  5. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden

Share This Page