Results 1 to 6 of 6
  1. #1
    tom2zip is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default help with randomizing integers

    when i run the code, not all numbers are displayed, sometimes 2 sometimes 3 (there are 4 lines in the text file)
    here's the code:
    Java Code:
    import java.io.*;
    import java.util.*;
    import java.util.Random.*;
    
    public class RandomOrder
    {
    	
    	
    	public static void main(String args[]) throws IOException
    	{
    		Scanner sc = new Scanner(new File("G:\\FINAL PROJECT\\program\\list.txt"));
    		int maxIndx = -1;
    		String text[] = new String[100];
    		
    		Random rndm = new Random();
    		
    		while(sc.hasNext())
    		{
    			maxIndx++;
    			text[maxIndx] = sc.nextLine();
    		}
    		sc.close();
    		
    		List<Integer> random = new ArrayList<Integer>(maxIndx + 1);
    		
    		for(int j=0; j<=maxIndx; j++)
    		{
    			int n = rndm.nextInt(maxIndx + 1);
    			
    			if(!random.contains(n))
    			{
    				random.add(n);
    				System.out.println(n + " " + text[n]);
    			}
    			
    		}
    		
    	}
    	
    	
    }
    Last edited by Norm; 11-27-2011 at 10:10 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: help with randomizing integers

    not all numbers are displayed, sometimes 2 sometimes 3 (there are 4 lines in the text file)
    What controls whether a line is displayed or not? Do you have any conditional statements that will skip over displaying a line?
    To see what your code is doing, add some printlns to display the values of all the variables that the program uses. The print out should show you how the code is executing.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: help with randomizing integers

    It is hard to determine what your problem is from the poor description provided.

    If your text array holds four objects then the max number of times the for loop can iterate is four. On the first iteration the random number is 1. Since it is not in the random List it will be added and the element at index 1 of text array is displayed. Then what happens on the 2nd, 3rd and 4th iteration if the random number is 1 each time? Nothing since 1 is already in the random List.

  4. #4
    tom2zip is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: help with randomizing integers

    here is what the text file looks like:
    aaaaaaaa aaaaaaaaaaaaaaaaaaaaa
    bbbbbbb bbbbbbbbbbbbbbbbbbbbb
    ccccccc cccccccccccccc
    dddd ddddddddddddddddddddddddddddddddddd

    and here is an example output:
    0 aaaaaaaa aaaaaaaaaaaaaaaaaaaaa
    3 dddd ddddddddddddddddddddddddddddddddddd
    1 bbbbbbb bbbbbbbbbbbbbbbbbbbbb

    as you can see, the element at index 2 is not displayed
    what im trying to achieve is to display all four elements without any of them repeating

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: help with randomizing integers

    What controls whether a line is displayed or not? Do you have any conditional statements that will skip over displaying a line?
    To see what your code is doing, add some printlns to display the values of all the variables that the program uses. The print out should show you how the code is executing.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: help with randomizing integers

    I explained what your problem is. One of the loop iterations the random number generated was either 0, 3 or 1. Since tyou have already generated that number previously it will not show it again. Then the for loop will exit since it has executed 4 times. Perhaps you should use a while loop instead and think about the exit condition.

Similar Threads

  1. Reverse the integers
    By aramiky818 in forum New To Java
    Replies: 3
    Last Post: 04-23-2011, 07:17 PM
  2. Help with Randomizing
    By Spyderpig in forum New To Java
    Replies: 9
    Last Post: 02-18-2011, 05:34 AM
  3. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 10:29 AM
  4. Difficulties randomizing images in an array
    By phb5004 in forum New To Java
    Replies: 2
    Last Post: 12-11-2009, 04:45 AM
  5. randomizing an array HELP! please!
    By hugolord in forum New To Java
    Replies: 12
    Last Post: 08-10-2008, 03:36 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
  •