Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Try/Catch exception so i can return an array and print out an exception too

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Try/Catch exception so i can return an array and print out an exception too

    Hi i am writing a lottery method where the user has to enter in two numbers, n and k, as arguments. The lottery gets filled with a randomized queue that goes up to k. so if i put in k=10 the queue would hold 1,2,3,4,5,6,7,8,9,10. The argument n is the number of items that has to be removed randomly. so if i chose 3 then it could return 4,6,8 or it could be 1,3,10.

    Now if n is greater than k it has to throw an error saying that there is not enough items in the queue to pull. So if i put n=5 and k=3, there are still 3 items in the queue but i can't select 5.

    Now my problem is i have to return the items that are still in the queue. so n=5 and k=3 would return 1,3,2 or 2,3,1 and so forth. But i have to print an exception after i return that array. So far i am able to return the array but i can not get the try catch exception to work. Is there another method i can try that will return the array and then print out the exception after that so it looks like this:

    %java Lottery 5 2
    2  1
    java.lang.Exception: Not enough items in your queue.
    at Lottery.pickNumbers(Lottery.java:29)
    at Lottery.main(Lottery.java:56)

    Here is my code


    import java.util.*;
    import java.math.*;
    public class Lottery{
        RandomizedQueue rq;
        Random Rnum = new Random();
        int [] Larray;
     
        // constructs a Lottery class
        public Lottery(){
        }
     
     
        // picks the numbers and store them in an array of integers
        // int n: number of items to pick
        // int k: maximum integer to be picked
        public int [] pickNumbers(int n, int k) throws Exception{
     
        	rq = new RandomizedQueue();
     
        	int [] remainQueue = new int [k];
     
     
        	if(n>k) 
        	{
        		for(int i=1; i<=remainQueue.length;i++)
        		{
        			rq.enqueue(i);
        		}
    	    		for(int i =0; i<remainQueue.length;i++)
    	    		{
    	    			try{
    	    			remainQueue[i] = rq.dequeue();
    	    			}
    	    			catch (Exception e)
    	    			{
    	    				System.out.println("not enough items in the queue");
    	    			}
    	    		}
    	    		return remainQueue;	
        	}  
     
        	for(int i =1;i<=k;i++)
        	{
        		rq.enqueue(i);
        	}
     
        	Larray = new int[n];
        	for(int i = 0;i< Larray.length;i++)
        	{
        		Larray[i] = rq.dequeue();
        	}
     
        	return Larray;
     
     
        }
     
     
     
        // Do not change main().
        public static void main(String [] args) throws Exception{
        	if (args.length<2){
    	       System.out.println("Please enter your input values.");
    	       System.out.println("e.g. java Lottery [number of integers to pick] [Maximum integer to be picked]");
    	   }else{
    	       int n = Integer.parseInt(args[0]);
    	       int k = Integer.parseInt(args[1]);
    	       Lottery l = new Lottery();
    	       try{
    		   int [] picked = l.pickNumbers(n,k);
    		   for (int i = 0; i< picked.length; i++){
    		       System.out.print(picked[i]+" ");
    		   }
    		   System.out.println();
    	       }catch (Exception e){
    		   e.printStackTrace();
    	       }
    	   }
     
     
     
        } 
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Try/Catch exception so i can return an array and print out an exception too

    You might want to check out the finally block.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Exception when creating an array
    By Majora94 in forum Collections and Generics
    Replies: 2
    Last Post: February 23rd, 2012, 05:33 PM
  2. [SOLVED] Catch an Exception right
    By beruska in forum Exceptions
    Replies: 2
    Last Post: October 25th, 2011, 12:48 PM
  3. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  4. [SOLVED] Problems with Try/Catch Catching Wrong Exception
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 10th, 2011, 08:08 PM
  5. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM