Results 1 to 6 of 6
Thread: help with randomizing integers
- 11-27-2011, 09:11 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
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
- 11-27-2011, 10:10 PM #2
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?not all numbers are displayed, sometimes 2 sometimes 3 (there are 4 lines in the text file)
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.
- 11-27-2011, 10:48 PM #3
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.
- 11-27-2011, 11:27 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
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
- 11-27-2011, 11:28 PM #5
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.
- 11-27-2011, 11:30 PM #6
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
-
Reverse the integers
By aramiky818 in forum New To JavaReplies: 3Last Post: 04-23-2011, 07:17 PM -
Help with Randomizing
By Spyderpig in forum New To JavaReplies: 9Last Post: 02-18-2011, 05:34 AM -
Set of Integers
By rsjava24 in forum New To JavaReplies: 7Last Post: 01-28-2010, 10:29 AM -
Difficulties randomizing images in an array
By phb5004 in forum New To JavaReplies: 2Last Post: 12-11-2009, 04:45 AM -
randomizing an array HELP! please!
By hugolord in forum New To JavaReplies: 12Last Post: 08-10-2008, 03:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks