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

    Exclamation changing my program to array working program

    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) 
      {	
    	
    	Random rand = 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 gennum  = rand.nextInt(999); 	//This is the maximum that the program generates the numbers to								
    									 
    		int finalnum = String.valueOf(gennum).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" + (gennum) + " "); 
    		}
    
    		if (finalnum == 2) //if the final number has two numbers so say 19 the program will add one zero
    		{					
               	System.out.print("0" + (gennum) + " ");
                	}
    			
    		if (finalnum == 3) //and if program has three numbers the program will just output the generated number
    		{					
               	System.out.print((gennum) + " ");
    		}
    
    		
    	   }
    
    	}
      }
    	
    }
    hi guys i have my program up and running no problems but i need to change it to an array program. im new to java and finding this part quite difficult any input would be appreciated alot

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

    Default

    I find it quite difficult too, as I have no idea what an array program is.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What do you want the array to be doing?
    Have you read an arrays tutorial yet?

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

    Default

    basically i want an array of 100 random numbers. I have yes but would like a lil guidence as to how i could alter my program to make it an a program with 100 random numbers using arrays

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

    Default

    any input would be really really helpful. thank you

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    First you declare and initialize the array.
    Then you assign values into it in the loop that is generating the numbers.

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

    Default

    hey r035198x many thanks for your speedy reply, i did try that originally but i encountered a few error messages in this process. do u want to know the error messages that i am encountering?

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

    Default

    Also post the code, the error messages are not helpful alone.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    hey phHein here is my code with attempted ammendments to it, to modify my code into an array.still getting errors. plz help lol

    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
    	initializeArray(rand);
        randomizeArray(rand);
    	
    	
    	for (int count = 0; count < 10; count++) //allocates the rows of 10 x 10 random numbers
    	{
    		System.out.println();
    
            	for (int i = 0; i < rand.length; i++) 
              {
    		int gennum  = rand.nextInt(999); 	//This is the maximum that the program generates the numbers to								
    									 
    		int finalnum = String.valueOf(gennum).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" + (gennum) + " "); 
    		}
    
    		if (finalnum == 2) //if the final number has two numbers so say 19 the program will add one zero
    		{					
               	System.out.print("0" + (gennum) + " ");
                	}
    			
    		if (finalnum == 3) //and if program has three numbers the program will just output the generated number
    		{					
               	System.out.print((gennum) + " ");
    		}
    
    		
    	   }
    
    	}
      }
    	
    }

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

    Default

    Where are the error messages?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    im using the cmd how can i print off the errors?

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

    Default

    Mark the output with the mouse, press enter and paste them here.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    M:\javalab\lab6>
    M:\javalab\lab6>javac Random100.java
    Random100.java:19: cannot find symbol
    symbol : method initializeArray(int[])
    location: class Random100
    initializeArray(rand);
    ^
    Random100.java:20: cannot find symbol
    symbol : method randomizeArray(int[])
    location: class Random100
    randomizeArray(rand);
    ^
    Random100.java:29: cannot find symbol
    symbol : method nextInt(int)
    location: class int[]
    int gennum = rand.nextInt(999); //This is the maximum th
    at the program generates the numbers to

    ^
    3 errors

    thanks alot =)
    Last edited by Chewart; 11-17-2009 at 03:19 PM. Reason: copyied all my cmd lol

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

    Default

    Quote Originally Posted by Chewart
    Random100.java:19: cannot find symbol
    symbol : method initializeArray(int[])
    location: class Random100
    initializeArray(rand);
    That method doesn't exist!

    Quote Originally Posted by Chewart
    Random100.java:20: cannot find symbol
    symbol : method randomizeArray(int[])
    location: class Random100
    randomizeArray(rand);
    That method doesn't exist either!
    Quote Originally Posted by Chewart
    Random100.java:29: cannot find symbol
    symbol : method nextInt(int)
    location: class int[]
    int gennum = rand.nextInt(999); //This is the maximum th
    at the program generates the numbers to
    rand is now an int[], that does not have a method called nextInt.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    honestly im really baffled on this program phHein. possibly could you have some input into my program as im not entirely understanding it fully :S

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

    Default

    Are you telling me that you don't understand your own program?!

    What exactly is your problem?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    thanks for the reply phHein. i do understand my program its just when im trying to implement the arrays in my program im not understanding fully. arrays is a part on the java language that ive only just been introduced to so, any lil bit of info that could possibly help me on my way to java program success in the future would be great =)

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

    Default

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    many thanks for the replies to my posts phHein. without telling me the right code, would i have to ammend my random number program that i have submitted alot in order to get my program working whilst using arrays??

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

    Default

    You're welcome. No, you dont need to change a lot. Go back to the code in your first reply and add an int[]. That should not break anything. Next you fill the array with those 100 randomly generated ints. If that doesn't break anything, use a new loop to print the contents of the array.

    That should get you started. If you get stuck, ask again.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Page 1 of 2 12 LastLast

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
  •