Page 2 of 2 FirstFirst 12
Results 21 to 40 of 40
  1. #21
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    many thanks.. i will post a lil bit later to let u know how im getting on!! many thanks agen =D

  2. #22
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,748
    Rep Power
    19

    Default

    You've already written some code that almost does what you want. So use that and instead of printing out the random number, stick it in the array instead.

    You haven't even attempted to write the code to populate your array yet (as shown by the lack of code for the two methods the compiler is complaining about). And yet the bulk of that code is already there in your earlier program.

    ETA: You lot type too fast!!
    :)

  3. #23
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    hey phHein ive been working on my program and i have got it compiling and to my amazement no compiler errors.. but the only problem i have, is that the program i have created is not generating any random numbers lol Hmm.. have you had alot of experience with java programming? and question for you also how long did it take you to get the hang of the java language when creating programs? below is what i have modified my previous program to try to output the random array numbers...

    Java Code:
    import java.util.Random;
    
    public class Random100
    {
    
      /*This is a program to generate random numbers between 
    	1 and 999 with no repetitions in the sequence */ 
     
      public static void main(String[] args) 
      {	
    	int[] rand = new int[100]; // one-dimensional array of possible sums
    	Random ran = new Random();
    	
    	
    	for (int count = 0; count < 10; count++) //allocates the rows of 10 x 10 random numbers
    	{
    		System.out.println();
    
        for (int i = 0; i < 10; i++) 
              {
              int finalnum = String.valueOf(ran).length();
    		int rannum  = ran.nextInt(999); 	//This is the maximum that the program generates the numbers to								
    									 
    		
    
    		if (finalnum == 1) //if the final number has one number so say 9 the program will add two zero's 
    		{				   
    		System.out.print("00" + (finalnum) + " "); 
    		}
    
    		if (finalnum == 2) //if the final number has two numbers so say 19 the program will add one zero
    		{					
               	System.out.print("0" + (finalnum) + " ");
                	}
    			
    		if (finalnum == 3) //and if program has three numbers the program will just output the generated number
    		{					
               	System.out.print((finalnum) + " ");
    		}
    
    		
    	   }
    
    	}
    	    	
      }
    	
    }

  4. #24
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Java Code:
    int finalnum = String.valueOf(ran).length(); 
    int rannum  = ran.nextInt(999);
    First you count the length of the random number that you generate in the next line?! And you count the length of the Random object there.

    Java Code:
    int rannum  = ran.nextInt(999);
    int finalnum = String.valueOf(rannum).length();
    Java experience? 8+ years.
    Getting the hang of it? Quickly, I had C and C++ experience, but I'm still learning.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #25
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,748
    Rep Power
    19

    Default

    You also aren't populating the array you've made with the random numbers.

    Forget about printing them out for now, just populate that array.

  6. #26
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    hey tolls could u possibly give me an example?

  7. #27
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    There are examples in the Array tutorial I've posted earlier.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #28
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    hey phHein i have been workin yet again on my program, i have come up with this i cnt seem to see where my array is implemented in my program. Can your experienced eye see what may be the problem?

    Java Code:
    import java.util.Random;
    
    public class Random100
    {
    
      /*This is a program to generate random numbers between 
    	1 and 999 with no repetitions in the sequence */ 
      public static void main(String[] args) 
      {
    	
    	int[] random1 = new int[100];
    	Random rand = new Random();
    	
    	for (int count = 0; count < 10; count++) 
    	{
    		System.out.println();
    
            	for (int i = 0; i < 10; i++) 
              {
    		int j = rand.nextInt(999); /*This is the maximum that								
    									the program generates the numbers to */
    		int finalnum = String.valueOf(j).length();
    
    		if (finalnum == 1) //if the final number has one number so say 9
    		{				   //the program will add two zero's 
    		System.out.print("00" + (j) + " "); 
    		}
    
    		if (finalnum == 2) //if the final number has two numbers so say 19
    		{					//the program will add one zero
               	System.out.print("0" + (j) + " ");
                	}
    			
    		if (finalnum == 3) //and if program has three numbers the program
    		{					// will just output the generated number
               	System.out.print((j) + " ");
    		}
    
    		
    	   }
    
    	}
      }
    	
    }

  9. #29
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Here your array is initialized.
    Java Code:
    int[] random1 = new int[100];
    As said before, next try to fill your array with the random numbers. You don't need a new method for that now.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  10. #30
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    hey phHein, i really cnt get my head around this program at the moment ive ammended it to best the best of my knowledge at present but cnt grasp it.. =S im getting more error messages

    dom100.main(Random100.java:16)

    6>javac Random100.java
    :29: incomparable types: java.ut
    if (rand == num1) //if the final
    ^
    :34: incomparable types: java.ut
    if (rand == num2) //if the final
    ^
    :39: incomparable types: java.ut
    if (rand == num3) //and if progra
    ^
    Java Code:
    public class Random100
    {
    
      /*This is a program to generate random numbers between 
    	1 and 999 with no repetitions in the sequence */ 
      public static void main(String[] args) 
      {
    	Random rand = new Random();
    	int[] random1 = new int[100];
    	int num1 = 1;
    	int num2 = 2;
    	int num3 = 3;
    	
    	for (int gen = 0; gen <1000;gen++)
    	{
    	++random1[rand.nextInt(999)];
    	}
    	
    	for (int count = 0; count < 10; count++) 
    	{
    		System.out.println();
    
            	for (int i = 0; i < 10; i++) 
              {
    		 /*This is the maximum that								
    									the program generates the numbers to */
    		for (int f = 1;f < random1.length;f++)
    		{
    		if (rand <= num1) //if the final number has one number so say 9
    		{				   //the program will add two zero's 
    		System.out.print("00" + (f) + " "); 
    		}
    
    		if (rand <= num2) //if the final number has two numbers so say 19
    		{					//the program will add one zero
               	System.out.print("0" + (f) + " ");
                	}
    			
    		if (rand <= num3) //and if program has three numbers the program
    		{					// will just output the generated number
               	System.out.print((f) + " ");
    		}
    
    		
    	   }
    
    	}
      }
    	
    }}
    any hints on what im missing?

  11. #31
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    hey phHein i have got my arrayed random program working at last( im so happy lol) but theres one problem i cant get 2 zeros for any number below 10 so 007 and any number below 100 one zero so 090.. please could you help me with this final bit =)

    Java Code:
    import java.util.Random;
    
    public class Ranny
    {
    	public static void main(String args[])
    	
    	{
     
    	int [] randarray = new int [100];
    	Random random = new Random();
    	
    	for (int i = 0; i<randarray.length;)
    	{	int temp = random.nextInt(1000);
    		randarray[i] = temp;
    		i++;
    			
    	}
    	
    	
    	for (int i = 0; i<randarray.length; i++)
    		System.out.print( randarray[i] + "\t");
    	}
    }

  12. #32
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    public class FormattingEg {
        public static void main(String[] args) {
            int i = 42;
            System.out.printf("|%d|%n", i);
            System.out.printf("|%5d|%n", i);
            System.out.printf("|%-5d|%n", i);
            System.out.printf("|%05d|%n", i);
        }
    }

    The | are just put in there so you can see what is printed (including spaces).

    The "|%05d|%n" things are called format strings. %n means "print a new line", "d" means decimal integer. The numbers in front of the "d" specify the minimum width you want the output to have.

    There are lots of options described in the Formatter documentation. They can be a bit daunting at first but it's worth mastering them a bit at a time as each new formatting requirement presents itself. (Similar functionality is available in lots of languages - C, PHP etc)

  13. #33
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    abosoluty brilliant pbrockway2 you have helped me alot ive completed my program now just need to tidy it up a bit now how cud i get 10 rows of 10 close together?
    Java Code:
    public class Random100
    {
    
      	/*This is a program to generate random numbers between 
    	1 and 999 with no repetitions in the sequence */ 
      
    	public static void main(String args[])
    	
     {
     
    	int [] randarray = new int [100];
    	Random random = new Random();
    	
    	for (int i = 0; i<randarray.length;)
    	{	int temp = random.nextInt(999);
    		randarray[i] = temp;
    		i++;
    			
    	}
    	
    	
    	for (int i = 0; i<randarray.length; i++)
    	{			  
    				   /* %03d\t, this generates the random numbers to
    				   three decimal places so the numbers are generated
    				   with a full number or number with 00's or one 0*/
    
    		System.out.printf("%03d\t", randarray[i]);
    	}
     
     }
    }

  14. #34
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Check i and if it has certain values print a new line. Remember the % operator.

    Edit: This addresses the "tidy up" question. Once it is tidied up something else might become apparent from testing. It shouldn't prove too difficult to correct if you apply the how-to-code step 0: "how would you do this by hand?"
    Last edited by pbrockway2; 11-17-2009 at 09:49 PM.

  15. #35
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    cheers dude!! =D any other hints for me

  16. #36
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,748
    Rep Power
    19

    Default

    Quote Originally Posted by Chewart View Post
    ( im so happy lol)
    And that's why we try not to simply hand out the code...:)

    Just one thing, your first for/loop should look like your second one. It's more normal to place the incrementing as the last part of the for statement rather than at the end of the loop itself. As you've done in the second loop, in fact, so I expect this is part of the "tidy up"...:)
    Last edited by Tolls; 11-18-2009 at 09:21 AM.

  17. #37
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    many thanks phHein i have completed my program =)

  18. #38
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Youre welcome, but r035198x, Tolls and pbrockway2 have also helped you.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  19. #39
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    i do apologise many thanks go out to 35198x, Tolls and pbrockway2 also =D

  20. #40
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Chewart View Post
    i do apologise many thanks go out to 35198x...
    ??

    The (*) SPNSr will hear about this.




    r035198x(<-----oppressed




    (*) Society for People with Names Starting with r

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 1
    Last Post: 08-27-2009, 01:20 PM
  2. Working on a menu program...using exceptions
    By Nightwarrior in forum New To Java
    Replies: 0
    Last Post: 04-16-2009, 04:40 AM
  3. Replies: 2
    Last Post: 03-16-2009, 07:52 PM
  4. [SOLVED] Changing the look of butons in program
    By linux1man in forum AWT / Swing
    Replies: 8
    Last Post: 03-14-2009, 06:04 PM
  5. My program is not working
    By MICHAELABICK in forum New To Java
    Replies: 6
    Last Post: 12-22-2008, 11:05 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
  •