Results 1 to 3 of 3
  1. #1
    psuhammer14 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default 1st Java program

    I am taking my 1st Java course this semester, and an assignment I have been given is giving me fits. Here is what I am trying to do, and what I have so far. I don't expect a full solution to the entire problem, but I am stuck part way through it, and an explanation/solution to what I am currently doing wrong would be appreciated.

    I am creating a lottery program that will compare 2 arrays of 6 numbers each, 1 inputted from the user, and another randomly generated. Numbers should be 1-40. I have to provide ways to prompt for invalid inputs, but I'm not there yet...

    I was able to create both arrays, and get them to successfully output to the screen. Now I have to display the arrays in sorted order, and that is where i am stuck.

    Below is my program so far... I apologize if this displays strangely, this is my first post on here, and I don't know if it will auto format to display the code correctly.
    Java Code:
    import java.util.*;
    
    public class InstantLottery
    {
    	static Scanner console = new Scanner(System.in);
    	public static void main(String[] args)
    	{
    		int sortedOrder;  //trying to initialize something to call from a method
    		
    		
    		
    		int[] playerNumbers = new int[6];    //creates the player array of 6 integers
    		for (int i=0; i < playerNumbers.length; i++)
    		{
    			System.out.print("Enter a number from 1-40 >");
    			playerNumbers[i] = console.nextInt();
    		}
    
    		
    		System.out.println("the numbers you entered are");//displays array
    		sortedOrder = LotteryMethods.selectionSort(playerNumbers);//attempt to call the sort method...
    		for (int i=0; i<playerNumbers.length; i++)//displays array
    		System.out.print(playerNumbers[i] +" ");//displays array
    		
    		
    
    		
    		
    		Random randNumGenerator = new Random();
    		int[] computerNumbers = new int[6];    //creates the computer array of 6 integers
    		
    		for (int i=0; i < computerNumbers.length; i++)
    		{
    		computerNumbers[i] = (randNumGenerator.nextInt(40)+1);
    		}
    
    		System.out.println("the numbers the computer picked are");//displays computer array
    		for (int i=0; i<computerNumbers.length; i++)//displays array
    		System.out.print(computerNumbers[i] +" ");//displays array
    		
    		
    	}
    	
    	
    	//lottery methods
    	
    	public class LotteryMethods
    	{
    		public static void selectionSort(int[] list, int listLength)
    		{
    			int index;
    			int smallestIndex;
    			int minIndex;
    			int temp;
    			
    			for (index=0; index<listLength-1; index++)
    			{
    				smallestIndex=index;
    				for(minIndex=index+1; minIndex<listLength; minIndex++)
    				if (list[minIndex]<list[smallestIndex]) smallestIndex=minIndex;
    				temp=list[smallestIndex];
    				list[smallestIndex]=list[index];
    				list[index]=temp;
    			}
    		}
    	}
    			
    }
    Last edited by psuhammer14; 12-12-2010 at 02:34 AM.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You have a problem here:
    Java Code:
                    if (list[minIndex]<list[smallestIndex]) smallestIndex=minIndex;
    That should be
    Java Code:
                    if (list[minIndex]<list[smallestIndex])
                    {
                        smallestIndex=minIndex;
    And that will make the rest of your } braces come out right. Also, you don't need the listLength parameter in your selectionSort method, as list will happily tell you what its length is.

    Beyond that, you should write a method to list an array of numbers, as you need to do that multiple times.
    Java Code:
        public void listNumbers(int[] list) {
             // TODO write this part
        }
    -Gary-

  3. #3
    psuhammer14 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default thank you

    thank you, that was a big help. the brackets were causing the problem. I changed it and it works now. off to finish the rest of it. Thanks again.

Similar Threads

  1. Replies: 8
    Last Post: 09-30-2010, 07:32 PM
  2. Replies: 4
    Last Post: 07-30-2010, 01:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 06:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •