Results 1 to 7 of 7

Thread: Randomizer

  1. #1
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Randomizer

    i am trying to save into an array yet it is not working for some reason... keeps giving me an error
    also i would like to generate a random number between 2 pre-set numbers (e.g. between 200 and 800), yet the method i used did not work what so ever, please help.


    Array code:
    Java Code:
    package Randomizer;
    
    import java.util.Random;
    
    public class randomizer 
    {
    
    	public static void main(String[] args) 
    	{
    		new randomizer();
    	}
    	
    	public randomizer()
    	{
    		int x[] = null;
    		int y,temp;
    		Random r = new Random();
    		
    		for(int i=0;i<100000;i++)
    		{
    			y = r.nextInt(1000);
    			x[i] = y;
    
    //			x[i] = r.nextInt(1000);  -- did not work
    
    		}
    		
    		for(int i=0; i<100000; i++)
    		{
    			for(int j=0; j<100000; j++)
    			{
    				if(x[i] > x[i+1])
    				{
    					temp   = x[i+1];
    					x[i+1] = x[i];
    					x[i]   = temp;
    				}
    			}
    		}
    		
    		System.out.println(x[1]+"/t"+x[99999]);
    	}
    }

    Error:
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at Randomizer.randomizer.<init>(randomizer.java:24)
    	at Randomizer.randomizer.main(randomizer.java:10)

  2. #2
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    78
    Rep Power
    0

    Default Re: Randomizer

    You need to do int x[] = new int[50] instead of = null. Of course change the 50 to something bigger to fit your needs
    Last edited by jbarke12; 05-09-2013 at 11:09 PM.

  3. #3
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    78
    Rep Power
    0

    Default Re: Randomizer

    I also don't understand what you are doing with the for loop with the j variable in it. You never use j.

  4. #4
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Re: Randomizer

    Thanks, i do not remember learning that you have to add the "new int [num];"

    and the "j" is used because it is nested and can not use "i" again, that part is for sorting since i wanted to see the what arae the highest and lowest numbers are going to be.

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Randomizer

    but the J loop does nothing but slow the application down, you dont reference j, so its doing a loop for no reason.

  6. #6
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Re: Randomizer

    I am trying to sort the integers.. you have to have 2 loops to do so..
    unless you have another way of doing it?

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,946
    Rep Power
    4

    Default Re: Randomizer

    monkejr97 is correct. Your inner loop is not doing anything.

    Here is one way to do a bubble sort.
    Java Code:
          int len = x.length;
          for (int i = 0; i < len - 1; i++) {
             for (int j = i + 1; j < len; j++) {
                if (x[i] > x[j]) {
                   int temp = x[i];
                   x[i] = x[j];
                   x[j] = temp;
                }
             }
          }
    However, bubble sorts are great for learning one's first sorting algorithm but they are notoriously slow. I suggest you check out
    Arrays.sort().

    Also, what do you want randomizer to do? I have a feeling there is a much better way to do it by using some of the JDK classes.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Need help adding a "randomizer" to this code.
    By bzbzb1 in forum New To Java
    Replies: 1
    Last Post: 05-15-2012, 05:49 AM
  2. Sudoku randomizer help
    By mochibon in forum New To Java
    Replies: 0
    Last Post: 04-07-2010, 03:05 AM

Posting Permissions

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