Help me with this Java?
March 28, 2010 by
Filed under class registration system
import java.awt.Component;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class OkTu
{
public static void main (String[] args)
{
String reply = “”;
int code = 0;
Scanner input = new Scanner(System.in);
while(true){
//enter luckydraw number
String userDraw = JOptionPane.showInputDialog(“Enter Your Lucky Number : “);
//enter user name
String userNameString = JOptionPane.showInputDialog(“Enter Your Name : “);
//enter user id
String userId = JOptionPane.showInputDialog(“Enter Your I/D No : “);
//enter user phone no
String userPhone = JOptionPane.showInputDialog(“Enter Your Phone No : +673…”);
//enter user email
String userEmail = JOptionPane.showInputDialog(“Enter Your Email : “);
//Anymore
int response = JOptionPane.showConfirmDialog(null, “Do you want to add another contestant?”, “Adding Contestants”,
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.YES_OPTION)
if (response == JOptionPane.NO_OPTION)break;
{
}
//display results
String output = “CONGRAGULATIONS!!!\nTHE WINNER IS..\n\n\n” + “Name: ” + userNameString + ” \n” + “I/D No.: ” + userId + ” \n” + “Phone No.: ” + userPhone + ” \n” + “E-mail : ” + userEmail;
}
}
}
—-
my group is doing some assignment about Lucky draw generator using JAVA and JOption. With registration of the contestants needed and then, after the registration, a number will be drawn and the name and lucky number of the winner will appear.
We haven’t add the mathRandom yet. We’re still trying to solve the problem where when there’s a message about adding contestants, there’s a Yes and No Option, but when the ‘No’ button is clicked it goes back to registering people. so what should we edit?
I’ll try and look at this more later, as I have to go to work. But I’ll give you some hints now:
1. You’re saying “while (true)”. While what is true? That makes no sense and is why you’re program never ends. You need a variable boolean want = true; or something above the while statement. And say while (want == true) instead, and if the user says no that make want=false;
2. Whenever you add a new contestant, it overwrites the last contestant in there since a variable can only hold one value. You’ll need an array for each of those contestants.
EDIT: Actually, since you don’t know how long it is going to be, you can’t use an array, you’ll have to use an ArrayList. You can use an Array if you’re allowed to ask how many contestants instead of asking if they would like to add another.
I think I’ve completed this program fully, I’m not sure if its 100% since I don’t have all the directions. The only thing is though I don’t know how you’re supposed to choose the random number. There’s no function I know of to choose a random number out of an array of the numbers in there. What I did was Choose a random number between 0 and the size of the userDraw ArrayList. And then I could print out the contents of the ArrayList using that random number.
For where you’re at now, you’ll do something like this:
ArrayList userDraw = new ArrayList ();
…..above the while statement. Do that for all the fields.
userDraw.add(JOptionPane.showInputDialog(“Enter Your Lucky Number : “));
…..inside the while statement, you’ll do this. Then it adds to ArrayList.