
using a do while loop to search array
Hi
I have got an array to generate random numbers and am now trying to introduce a boolean expression so that when the numbers are being generating, if a number is found to be in the array already another number is generated.
I cannot compile the code below, as it says the for the } just after the while line "Syntx error, insert "while(Expression);" to complete DoStatement"
Can anyone point me in the right direction with what I have done wrong?
Does my code look like it will do what i want it to?
Any help greatly appreciated  thank you
My code is:
Code:
public class BinaryTreeAction
{
public TreeNode tn; // graphical representation of the node
public TreeNode torch; // torch pointer to node
public int NodeSize = 25;
int amount = 6; // number of integers needed to generate
int[] nums = new int[amount];
int i;
// method for searching the array for duplicate numbers
boolean fresh (int [] nums, int val){
for (int i = 0; i < amount; i++){
if (nums[i] == val) return true;
}
{
return false;
}
}// end fresh method
//method to generate random numbers to insert into the tree
public void randomNumber(){
for (int i=0; i<amount; i++){
do{
nums[i] = (int) Math.floor(Math.random() * 100);
// generate numbers between 0 and 99
nums[i] = nums[i] 50;
// minus 50 from the random number generated so that some of the final numbers are negative.
for (int i = 0; i<nums.length; i++){
System.out.println("random is " + nums[i]);
}
while (not(fresh(nums,i)));
}
}// end of randomNumber method
Last edited by JavaSado; 08022005 at 09:45 AM.

Hmm....
I haven't dived into the logics here but this piece of code has only the error:
Code:
"BinaryTreeAction.java": Error #: 300 : class TreeNode not found in class BinaryTreeAction at line 4, column 10
Here is the code:
Code:
public class BinaryTreeAction {
public TreeNode tn; // graphical representation of the node
public TreeNode torch; // torch pointer to node
public int NodeSize = 25;
int amount = 6; // number of integers needed to generate
int[] nums = new int[amount];
int i;
// method for searching the array for duplicate numbers
boolean fresh(int[] nums, int val) {
for (int i = 0; i < amount; i++) {
if (nums[i] == val)
return true;
}
{
return false;
}
} // end fresh method
//method to generate random numbers to insert into the tree
public void randomNumber() {
for (int i = 0; i < amount; i++) {
do {
nums[i] = (int) Math.floor(Math.random() * 100);
// generate numbers between 0 and 99
nums[i] = nums[i]  50;
// minus 50 from the random number generated so that some of the final numbers are negative.
for (int j = 0; j < nums.length; j++) {
System.out.println("random is " + nums[j]);
}
} while (!fresh(nums, i));
}
} // end of randomNumber method
}
PS:java has not got any not operator, it uses !,  java books are not boring
Last edited by sjalle; 08022005 at 10:11 AM.
eschew obfuscation

Hi
Thanks for that  oops have change the not for ! but still get the same error.
I agree Java books are not boring and I have a few around me at the moment and I have been looking on the internet for help on using these loops for searching arrays befpre posting this thread, but haven't been able to find the answer  so I was hoping I someone here could help me
It may not seem like it but I do spend a long time on these problems before posting them.
Hope someone can help.
Sorry about the error you got, it should have commented out the lines on the treenode as they refer to another class  sorry

removed the error now and it is now compiling and running OK.
Code:
int amount = 6; // number of integers needed to generate
int[] nums = new int[amount];
int i;
// method for searching the array for duplicate numbers
boolean fresh (int [] nums, int val){
for (int i = 0; i < amount; i++){
if (nums[i] == val) return true;
}
{
return false;
}
}// end fresh method
// method to generate random numbers to insert into the tree
public void randomNumber()
{
for (int i=0; i<amount; i++) {
do
{
nums[i] = (int) Math.floor(Math.random() * 100); // generate numbers between 0 and 99
nums[i] = nums[i] 50; // minus 50 from the random number generated so that some of the final numbers are negative.
System.out.println("random is " + nums[i]);
}
while(!(fresh(nums,i)));
}
}// end of randomNumber method
its not quite doing what I need but will look into it a bit more first.
thanks for your initial response, it got me onto the right track.

Just a comment, even if it is not directly related.
nums[i] = (int) Math.floor(Math.random() * 100);
The Math.floor is redundant.
The integer cast will discard the fractional portion already, so if you cast 3.9999 to an int, you get 3.
There is a round function available if 4 is the desired result.

Hi deroga
Thanks very much for the comment  very useful
Luckily this time I am not bothered which integer it outputs as long as it is an integer. I am just after some random numbers to insert into my binary search tree!!
Similar Threads

By kanakatam in forum Java
Replies: 2
Last Post: 04152005, 10:06 PM

By Phil O Reilly in forum Java
Replies: 4
Last Post: 03122005, 01:57 PM

By Larry Serflaten in forum Talk to the Editors
Replies: 3
Last Post: 02042002, 11:54 AM

By Toms Ng in forum Database
Replies: 1
Last Post: 12182001, 10:08 AM

By Larry Serflaten in forum Talk to the Editors
Replies: 3
Last Post: 07162001, 03:30 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

Forum Rules

Development Centers
 Android Development Center
 Cloud Development Project Center
 HTML5 Development Center
 Windows Mobile Development Center
