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 4 of 4

Thread: Why is the output always "0"?

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

    Default Why is the output always "0"?

    So I am trying to write a Java program that simulates a Powerball ticket.

    It uses the Random function to generation 5 numbers, followed by a special number within a different range.

    It uses arrays for everything.

    The first 5 numbers are randomized nicely, but the "Special Number" is always 0.
    Can someone please help me to figure out why?

    Thanks

    import java.util.Random;
     
    public class Test
    {
    	public static void main(String args[])
    	{
    		final int NUM_row = 4;
    		final int Num_columns=6;
    		int[][] numbers = new int[NUM_row][Num_columns];
    		Random randomNumbers = new Random();
    		int row, columns;
     
    		final int PowerNUM_row = 4;
    		final int PowerNum_columns=5;
    		int[][] Powernumbers = new int[PowerNUM_row][PowerNum_columns];
    		Random PowerrandomNumbers = new Random();
    		int Powerrow, Powercolumns=0 ;
     
    		for (Powerrow = 0; Powerrow < 3; Powerrow++)
    			for (Powercolumns = 0; Powercolumns < 1; Powercolumns++)
    				Powernumbers[Powerrow][Powercolumns] = PowerrandomNumbers.nextInt(12);
     
    		for (row = 0; row < 3; row++)
    			for (columns = 0; columns < 5; columns++)
    				numbers[row][columns] = randomNumbers.nextInt(49); 
     
    		System.out.printf("Power Ball Ticket: \n\n");
    		for (row = 0; row < 1; row++)
    		{
    			for (columns = 0; columns < 5; columns++)
    			{
    				System.out.printf("%d ",numbers[row][columns]);
    			}
    			System.out.printf("Power Ball: %d", Powernumbers[Powerrow][Powercolumns]);
    			System.out.println();
    		}
    	}
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Why is the output always "0"?

    import java.util.Random;
     
    public class Test
    {
    	public static void main(String args[])
    	{
    		final int NUM_row = 4;// Note NUM is all caps vs below....
    		final int Num_columns=6;// You should stick to one style, not NUM and Num
    		int[][] numbers = new int[NUM_row][Num_columns];
    		Random randomNumbers = new Random();
    		int row, columns;
     
    		final int PowerNUM_row = 4;
    		final int PowerNum_columns=5;
    		int[][] Powernumbers = new int[PowerNUM_row][PowerNum_columns];
    		Random PowerrandomNumbers = new Random();
    		int Powerrow, Powercolumns=0 ;
     
    		for (Powerrow = 0; Powerrow < 3; Powerrow++)// What is this 3 here?
    			for (Powercolumns = 0; Powercolumns < 1; Powercolumns++)//...and this 1?
    				Powernumbers[Powerrow][Powercolumns] = PowerrandomNumbers.nextInt(12);//..and this 12?
     
    		for (row = 0; row < 3; row++)//..and this 3>
    			for (columns = 0; columns < 5; columns++)//...and this 5>
    				numbers[row][columns] = randomNumbers.nextInt(49); //and this 49?
     
    		System.out.printf("Power Ball Ticket: \n\n");
    		for (row = 0; row < 1; row++)//...and this 1?
    		{
    			for (columns = 0; columns < 5; columns++) //...and this 5?
    			{
    				System.out.printf("%d ",numbers[row][columns]);
    			}
    			System.out.printf("Power Ball: %d", Powernumbers[Powerrow][Powercolumns]);
    			System.out.println();
    		}
    	}
    }
    My first suggestion is to clean up all of those magical numbers. Here is the reason why. As I look over your code to see what is going on, it is not easy to follow. To you, now since its fresh, it may be clear. But to someone else, and to you after you have done other things, this will be hard to read. Replace the magic numbers with variables of good names, and maybe the error will be easier to spot.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Why is the output always "0"?

    Quote Originally Posted by TheBattousaixx View Post
    ...help me to figure out...
    Make the program tell you what it is writing to and reading from the Powernumbers array:
            for (Powerrow = 0; Powerrow < 3; Powerrow++)
            {
                for (Powercolumns = 0; Powercolumns < 1; Powercolumns++)
                {
                    Powernumbers[Powerrow][Powercolumns] = PowerrandomNumbers.nextInt(12);
     
                    // For debugging
                    System.out.printf("\n 1: Powernumbers[%d][%d] = %d\n",
                            Powerrow, Powercolumns, Powernumbers[Powerrow][Powercolumns]);
     
                }
            }
    .
    .
    .
            for (row = 0; row < 1; row++)
            {
                for (columns = 0; columns < 5; columns++)
                {
                    System.out.printf("%d ",numbers[row][columns]);
                }
     
                // For debugging
                System.out.printf("\n 2: Powernumbers[%d][%d] = %d\n",
                        Powerrow, Powercolumns, Powernumbers[Powerrow][Powercolumns]);
     
                System.out.printf("Power Ball: %d", Powernumbers[Powerrow][Powercolumns]);
                System.out.println();
            }


    Cheers!

    Z
    Last edited by Zaphod_b; July 24th, 2012 at 04:14 PM.

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Why is the output always "0"?

    public class Test
    {
    	public static void main(String args[])
    	{
    		final int NUM_row = 4;
    		final int Num_columns=6;
    		int[][] numbers = new int[NUM_row][Num_columns];
    		Random randomNumbers = new Random();
    		int row, columns;
     
    		final int PowerNUM_row = 4;
    		final int PowerNum_columns=5;
    		int[][] Powernumbers = new int[PowerNUM_row][PowerNum_columns];
    		Random PowerrandomNumbers = new Random();
    		int Powerrow, Powercolumns=0 ;
     
    		for (Powerrow = 0; Powerrow < 3; Powerrow++)
    			for (Powercolumns = 0; Powercolumns < 1; Powercolumns++)
    				Powernumbers[Powerrow][Powercolumns] = PowerrandomNumbers.nextInt(12);
                    /* now that the loop is over, Powerrow = 3, Powercolumns = 1 */
     
    		for (row = 0; row < 3; row++)
    			for (columns = 0; columns < 5; columns++)
    				numbers[row][columns] = randomNumbers.nextInt(49); 
     
    		System.out.printf("Power Ball Ticket: \n\n");
    		for (row = 0; row < 1; row++)
    		{
    			for (columns = 0; columns < 5; columns++)
    			{
    				System.out.printf("%d ",numbers[row][columns]);
    			}
                            /*
                               Powerrow, Powercolumns not reset, so this prints Powernumbers[3][1]
                               which is never modified after initialization, and therefore 0
                             */
    			System.out.printf("Power Ball: %d", Powernumbers[Powerrow][Powercolumns]);
    			System.out.println();
    		}
    	}
    }
    Hopefully that clears it up for you. I suggest you write cleaner code in the future where your logic is easier to follow. Your loops when generating the Powernumbers modify Powerrow and Powercolumns until they reach 3 and 1, respectively. Since Powernumbers[3][1] is never modified at any intermittent point, the value, when printed, will be that default from initialization i.e. 0.
    Last edited by veeer; July 25th, 2012 at 08:00 AM.

Similar Threads

  1. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM