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: Optimize/Improvements/Suggestions for my code

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Optimize/Improvements/Suggestions for my code

    Hi Everyone,
    New here and picking up Java little by little. I just finished writing a SuperLotto program and it works fine, but feel like the code can be improve upon. Any suggestions, tips, etc... on what I can do to improve on my code would be appreciated!

    I tried using a standard array,
    int ballNumbers = new int[6];
    since I know I don't need anything more then 6 balls #, but when I did that, I couldn't use ballNumber.contains().

    Thanks in advance!

    import java.util.*;
     
    class Lotto {
     
    	public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
     
    		Random lottoBall = new Random();
     
    		int play,			// # of quick pick ticket player wants to purchase
    			count=0,		// once count == play, exit program
    			number,			// variable to hold the random number between 1-47
    			ballCount=0;	// SuperLotto has 6 balls total + Mega Number
     
    		ArrayList<Integer> ballNumbers = new ArrayList<Integer>(); // array to hold each ball #
     
    		System.out.print("Enter # of plays: ");
    		play = input.nextInt();
     
    		while(count<play) {
    			while(ballCount < 6) {
    				number = 1+lottoBall.nextInt(47);
     
    				// check for duplicate numbers
    				if(!ballNumbers.contains(number)) {
    					ballNumbers.add(number);
    					ballCount++;
    				}
    			}
     
    			// sort array
    			Collections.sort(ballNumbers);
     
    			// display array content
    			for(int i=0; i<ballNumbers.size();i++)
    				System.out.print(ballNumbers.get(i) + " ");
     
    			// clear array
    			ballNumbers.clear();
     
    			// display Mega Number ball #
    			System.out.printf("\nMega Number: %d\n\n", 1+lottoBall.nextInt(27));
     
    			ballCount = 0;
    			count++;
    		}		
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Optimize/Improvements/Suggestions for my code

    If you wanted to use Arrays you could always write you're own Contains method. Just a simple boolean method that loops through the list and returns true if it's found would do. Benefit of arrays is that it can be faster. Don't think it would really make a difference with your program though. Also, it's not the best idea to cram everything into your main method. Why not have a class called game that creates a new instance for each go.
    Last edited by Diplo; July 29th, 2011 at 04:19 PM.

Similar Threads

  1. returns all suggestions
    By mario_tim in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2011, 05:51 AM
  2. Suggestions how to do this?
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: September 2nd, 2010, 01:18 PM