#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    london
    Posts
    1
    Rep Power
    0

    Question problems ? can some one tell me whats wrong with this code


    //shows the result of the lottery numbers for the week
    import javax.swing.JOptionPane; // import class JOptionPane
    import java.util.StringTokenizer;//import class string token utility

    //declares class lottery numbers
    public class lotteryNumbers
    {
    public static void resultLottery()
    {
    // declaration of var
    int number1=0,number3=0,number5=0,number2=0,number4=0, number6=0;

    String userInput="1 2 3 4 5 6";
    int jackpot[]={9,12,19,21,24,32};
    int array[]=new int[6];
    int n=0;
    // passes a user input to a string to populate an array
    StringTokenizer token=new StringTokenizer(userInput);

    while(token.hasMoreTokens())
    {
    array[n]=Integer.parseInt(token.nextToken());
    n++;
    }
    //gets the first input from the user and store it in user input if it a
    // valid number
    while (number1<=0||number1>49)
    {
    userInput=JOptionPane.showInputDialog("please enter first number");
    number1=Integer.parseInt(userInput);


    if (number1<=0||number1>49)
    JOptionPane.showMessageDialog(null,"out of range number found please press enter to continue");
    else
    JOptionPane.showMessageDialog(null,"your first number is " + number1);
    }
    //gets the second input from the user and store it in user input if it a
    // valid number
    while (number2<=0||number2>49)
    {
    userInput=JOptionPane.showInputDialog("please enter second number");
    number2=Integer.parseInt(userInput);


    if (number2<=0||number2>49)
    JOptionPane.showMessageDialog(null,"out of range number found please press enter to continue");
    else
    JOptionPane.showMessageDialog(null,"your second number is " + number2);
    }
    //gets the third input from the user and store it in user input if it a
    // valid number
    while (number3<=0||number3>49)
    {
    userInput=JOptionPane.showInputDialog("please enter third number");
    number3=Integer.parseInt(userInput);


    if (number3<=0||number3>49)
    JOptionPane.showMessageDialog(null,"out of range number found please press enter to continue");
    else
    JOptionPane.showMessageDialog(null,"your third number is "+ number3);
    }

    //gets the fourth input from the user and store it in user input if it a
    // valid number
    while (number4<=0||number4>49)
    {
    userInput=JOptionPane.showInputDialog("please enter fourth number");
    number4=Integer.parseInt(userInput);


    if (number4<=0||number4>49)
    JOptionPane.showMessageDialog(null,"out of range number found please press enter to continue");
    else
    JOptionPane.showMessageDialog(null,"your fourth number is " + number4);
    }
    //gets the fifth input from the user and store it in user input if it a
    // valid number
    while (number5<=0||number5>49)
    {
    userInput=JOptionPane.showInputDialog("please enter fifth number");
    number5=Integer.parseInt(userInput);

    if (number5<=0||number5>49)
    JOptionPane.showMessageDialog(null,"out of range number found please press enter to continue");
    else
    JOptionPane.showMessageDialog(null,"your fifth number is " + number5);
    }
    //gets the sixth input from the user and store it in user input if it a
    // valid number
    while (number6<=0||number6>49)
    {
    userInput=JOptionPane.showInputDialog("please enter sixth number");
    number6=Integer.parseInt(userInput);

    if (number6<=0||number6>49)
    JOptionPane.showMessageDialog(null,"out of range number found please press enter to continue");
    else
    JOptionPane.showMessageDialog(null,"your sixth number is "+ number6);
    }
    // loop to check winning numbers in the
    for( int i=0; i<6; i++ ) {
    if( jackpot[ i ]== int array[] )
    JOptionPane.showMessageDialog(null,"congrates your a millionare " +
    userInput + " at location " + i );
    else
    JOptionPane.showMessageDialog(null,"sorry please try again next week");
    }





    }

    //
    public static void main( String args[] )
    {
    resultLottery();

    System.exit(0);

    }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Greece
    Posts
    63
    Rep Power
    16

    This is not a "debug" but this wroks perfectly


    //_Test.java
    Code:
    import java.io.*;
    
    public class _Test
    {
    	public _Test()
    	{	}
    	
    	public static void main (String[] args) 
    	{
    		_Test pin = new _Test();		
    	    
    	    try
    	    {
    	    	pin.test1();
    	    	pin.test2();
    	    	pin.test3();
        	     	System.out.println("You got All 6");
        	    }
        	catch (_Negative e)
        	{
        		System.out.println(e.getMessage());
        	}
        	   	 
    	}
    
    	public void test1() throws _Negative
    	{
    		DataInputStream in = new DataInputStream(System.in);
    	    String line;
    	    int n;
    	    for (int i=1 ; i<=6; i++)
    	    {
    	    	try
    	    	{
    	    		System.out.println("N"+i+": ");
    	    		line=in.readLine();
    	    		n=Integer.valueOf(line).intValue();
    	    		if (n < 0) throw new _Negative("No negative numbers allow") ;
    	    		if (n > 49) throw new _Negative("Tooo large");
    	    		if (n==0) throw new _Negative ("no zero allow here");
    	    		six[SIX-i]=n;
    			}
    			catch (IOException e)
    			{
    				System.out.println("IO ERROR");
    			}
    	    }
    	}
    	
    	public void test2() throws _Negative
    	{
    		for (int i=0 ; i < SIX; i++)
    			for(int j=i; j< SIX; j++)
    				if (six[i]==six[j] && i!=j) throw new _Negative("Same numbers");
    	}
    	
    	public void test3() throws _Negative
    	{
    		int lott[] = new int [SIX];
    		
    		for(int i=0; i<SIX; i++)
    		{
    			lott[i]=((int)(48*Math.random()))+1;
    			System.out.println(lott[i]);
    		}		
    	
    		for(int i=0; i<SIX; i++)
    		{
    			if (ismember(lott[i],six))
    			;
    			else throw new _Negative("No lottary");
    		}
    		
    	}
    	
    	public boolean ismember(int a, int b[])
    	{
    		for(int i=0; i<b.length; i++)
    			if (a==b[i])
    				return true;
    		return false;
    	}
    	
    	private static final int SIX = 6;
    	private int[] six = new int[SIX];	
    }
    // _Negative.java
    Code:
    public class _Negative extends Exception
    {
    	public _Negative()
    	{
    		super("Negative");
    	}
    	
    	public _Negative(String message)
    	{
    		super(message);
    	}
    }
    Last edited by tony825; November 22nd, 2003 at 03:04 PM.
    No sign

IMN logo majestic logo threadwatch logo seochat tools logo