Printing Array Contents As They Are Being Added Random

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 500 Views - Last Post: 21 November 2012 - 11:41 AM Rate Topic: -----

#1 gpjones20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 12

Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 03:49 AM

So far i've created my program which has an array of 50 length. I need to add 50 random values to the array and they cant match. This part is ok and working.

My program loops through the array at its final stage using 2 for loops, One prints out 10 blank new lines the other prints using a for loop and does this 5 times i end up with a 5 x 10 table.

What i need to do now is figure a way to implement this in the main while loop so that the numbers are printed in a table format as they are generated. Rather than ging through the array again.

Here is my code:


/*
Program Title: Random50
Author: gpjones20
Created: 18 November 2012
Version: 1.0
*/

//random Imported To Program
import java.util.Random;
    
public class Random50
{
  public static void main(String args[])
  {
	//Creating New Random Object
    Random randomNum = new Random();
    
    int numbersArray [] = new int[50];
	
    int count = 0;
	
    System.out.println("");
	
	//Loop to Verify That No Matching numbers Are Added To Array
    while (count < 50)
    {
      int nextRandomVal = randomNum.nextInt(999) + 1;
		
       Boolean matchCheck = false;
		
       for (int i = 0; i < count; i++)
       {
		  //Checking For Equallity of Array Values With new radnom number
          if (numbersArray[i] == nextRandomVal)
          {
              matchCheck = true;
              //Un Comment Below Statement To See Matches While Prgram Is Running
              //System.out.println("A Match Has Been Found  " + numbersArray[i]);
              break;
          }
        }
	
	  //Adding Random Values To Array
      if (!matchCheck)
      {
        numbersArray[count] = nextRandomVal;
        count++;
      }
		
   }
 	 
	//Printing Message On To Console For User
    System.out.println("");
    System.out.println("Random Numbers Printing...........");
    System.out.println("");

    //Printing The Array Contents Table Format
	//Loop To print 10 Lines Of Print Statements
    for (int row = 0; row < 10; row++)
    {
		//Printing 5 indexes from the Array
        for(int column = 0; column < 5; column++)
        {
            System.out.printf("%03d ",numbersArray[row * 5 + column]);
        }
	    
		//Printing a new blank line
        System.out.println();
    }

  } 
} 

//END OF PROGRAM



Is This A Good Question/Topic? 0
  • +

Replies To: Printing Array Contents As They Are Being Added Random

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 04:01 AM

So in your main() method, each time a new value is found, print it. If the value is the 11th, 21st, 31st, or 41st (there's a pattern), print a linefeed before printing the value.
Was This Post Helpful? 0
  • +
  • -

#3 JHol  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 10-February 11

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 04:03 AM

create a function()
and call it from main

ex:

public void drawTable(){
for i = 0; i<10
print

}
Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 04:06 AM

View PostJHol, on 21 November 2012 - 04:33 PM, said:

create a function()
and call it from main

ex:

public void drawTable(){
for i = 0; i<10
print

}


I believe you need to use brackets for your for loop and provide an increment condition as well.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#5 gpjones20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 12

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 04:14 AM

Im not allowed to use methods. Thanks people for helping
Was This Post Helpful? 0
  • +
  • -

#6 gpjones20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 12

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 04:38 AM

Okay a quick update ive got it to print the values as the are added i think, Now getting the 5 x 10 grid to work anyone know how i could do it now

Code:

/*
Program Title: Random50
Author: gpjones20
Created: 18 November 2012
Version: 1.0
*/

//random Imported To Program
import java.util.Random;
    
public class Random50
{
  public static void main(String args[])
  {
	//Creating New Random Object
    Random randomNum = new Random();
    int numbersArray [] = new int[50];
    int count = 0;
	

    //Loop to Verify That No Matching numbers Are Added To Array
    while (count < 50)
    {
      int nextRandomVal = randomNum.nextInt(999) + 1;
		
       Boolean matchCheck = false;
		
       for (int i = 0; i < count; i++)
       {
		  //Checking For Equallity of Array Values With new radnom number
          if (numbersArray[i] == nextRandomVal)
          {
              matchCheck = true;
              //Un Comment Below Statement To See Matches While Prgram Is Running
              //System.out.println("A Match Has Been Found  " + numbersArray[i]);
			  //System.out.println();
              break;
          }
        }
	
      //Adding Random Values To Array
      if (!matchCheck)
      {
        numbersArray[count] = nextRandomVal;
		
		System.out.printf("%03d ",numbersArray[count]);
		System.out.println();
		
		
		count++;
      }
	  
		
   }

  } 
} 

//END OF PROGRAM


Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 04:40 AM

You only need one loop:

        for (int i = 1; i <= 50; i++) {
            System.out.printf("%03d ", a[i - 1]);

            if ((i % 5) == 0) {
                System.out.println();
            }
        }

Was This Post Helpful? 0
  • +
  • -

#8 JHol  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 10-February 11

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 05:08 AM

could this do the trick?
int currentRandom=randomNum.nextInt(999);
    boolean hasItem=false;
    java.util.ArrayList usedRandoms = new java.util.ArrayList();
    for (int row = 0; row < 10; row++)
    {
    	
        for(int column = 0; column < 5; column++)
        {	while(!hasItem){
        	
        	if(usedRandoms.contains(currentRandom)){
        		currentRandom = randomNum.nextInt(999);
        	}else{
        		usedRandoms.add(currentRandom);
        		hasItem=true;
        		break;
        	}
        }
        	
        	hasItem=false;
            System.out.printf("%03d ",currentRandom);
        }
	


Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 05:14 AM

Quote

java.util.ArrayList usedRandoms = new java.util.ArrayList();
If you're going to use a Collection then you might as well use Set (can't contain duplicates)
Was This Post Helpful? 1
  • +
  • -

#10 gpjones20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 12

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 10:29 AM

All of the code above is abit too complicated to what i have learned so far, Its something small im the below code thats wrong.


    //Adding Random Values To Array
42	      if (!matchCheck)
43	      {
44	        numbersArray[count] = nextRandomVal;
45	         
46	        System.out.printf("%03d ",numbersArray[count]);
47	        System.out.println();
48	         
49	         
50	        count++;
51	      }



Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 10:58 AM

INSIDE that loop, you'll want to check count and add a line feed every 10 numbers:
if ( count % 10 == 0 )
{
    System.out.println();
}

(I'm not sure count is the right number to use, but the idea's the same.)
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 10:58 AM

I'm confused. I thought your current problem was printing out the table in the correct format..? If that's the case, i've already shown you how to do that with one loop

This post has been edited by g00se: 21 November 2012 - 10:59 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#13 gpjones20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 12

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 11:09 AM

View Postg00se, on 21 November 2012 - 10:58 AM, said:

I'm confused. I thought your current problem was printing out the table in the correct format..? If that's the case, i've already shown you how to do that with one loop



It doesnt work though ill post up my code now 2 minutes

Here It is This Doesnt Work It Goes Mad!!

/*
Program Title: Random50
Author: Gethyn Pritchard Jones
Created: 18 November 2012
Version: 1.0
*/

//random Imported To Program
import java.util.Random;
    
public class Random50
{
  public static void main(String args[])
  {
	//Creating New Random Object
    Random randomNum = new Random();
    
    int numbersArray [] = new int[50];
	
    int count = 0;
	
	//Loop to Verify That No Matching numbers Are Added To Array
    while (count < 50)
    {
      int nextRandomVal = randomNum.nextInt(999) + 1;
		
       Boolean matchCheck = false;
		
       for (int i = 0; i < count; i++)
       {
		  //Checking For Equallity of Array Values With new radnom number
          if (numbersArray[i] == nextRandomVal)
          {
              matchCheck = true;
              break;
          }
        }
	  
	  //Adding Random Values To Array
      if (!matchCheck)
      {
        numbersArray[count] = nextRandomVal;
        count++;
      }
	  
	  for(int i = 1; i < 50 ; i++)
	  {
		System.out.printf("%03d ", numbersArray[i]);
		
		if((i % 5) == 0)
		{
			System.out.println();	
		}
	  }
		
	}
  } 
} 

//END OF PROGRAM




Here It is This Doesnt Work It Goes Mad!!

/*
Program Title: Random50
Author: Gethyn Pritchard Jones
Created: 18 November 2012
Version: 1.0
*/

//random Imported To Program
import java.util.Random;
    
public class Random50
{
  public static void main(String args[])
  {
	//Creating New Random Object
    Random randomNum = new Random();
    
    int numbersArray [] = new int[50];
	
    int count = 0;
	
	//Loop to Verify That No Matching numbers Are Added To Array
    while (count < 50)
    {
      int nextRandomVal = randomNum.nextInt(999) + 1;
		
       Boolean matchCheck = false;
		
       for (int i = 0; i < count; i++)
       {
		  //Checking For Equallity of Array Values With new radnom number
          if (numbersArray[i] == nextRandomVal)
          {
              matchCheck = true;
              break;
          }
        }
	  
	  //Adding Random Values To Array
      if (!matchCheck)
      {
        numbersArray[count] = nextRandomVal;
        count++;
      }
	  
	  for(int i = 1; i < 50 ; i++)
	  {
		System.out.printf("%03d ", numbersArray[i]);
		
		if((i % 5) == 0)
		{
			System.out.println();	
		}
	  }
		
	}
  } 
} 

//END OF PROGRAM



Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 11:21 AM

You've got your table loop in the wrong place. It should be at the end, unnested in any other loop
Was This Post Helpful? 0
  • +
  • -

#15 gpjones20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 12

Re: Printing Array Contents As They Are Being Added Random

Posted 21 November 2012 - 11:29 AM

View Postg00se, on 21 November 2012 - 11:21 AM, said:

You've got your table loop in the wrong place. It should be at the end, unnested in any other loop


The thing is the printing has to be done while the numbers are being generated and put in to the array. Heres a better description for you:

I have to print the numbers in table format in a second loop which follows the first while loop. However, you should ultimately place the print statements within the main outer loop so that the program displays values as they are accepted. This approach avoids having to traverse the array for a second time.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2