using a do while loop to search array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: using a do while loop to search array

  1. #1
    Join Date
    Jul 2005
    Posts
    32

    Question 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; 08-02-2005 at 09:45 AM.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    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; 08-02-2005 at 10:11 AM.
    eschew obfuscation

  3. #3
    Join Date
    Jul 2005
    Posts
    32
    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

  4. #4
    Join Date
    Jul 2005
    Posts
    32
    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.

  5. #5
    Join Date
    Aug 2005
    Posts
    5
    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.

  6. #6
    Join Date
    Jul 2005
    Posts
    32
    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

  1. Replies: 2
    Last Post: 04-15-2005, 10:06 PM
  2. Loop Reading To Array?
    By Phil O Reilly in forum Java
    Replies: 4
    Last Post: 03-12-2005, 01:57 PM
  3. What's up with the Devx search engine???
    By Larry Serflaten in forum Talk to the Editors
    Replies: 3
    Last Post: 02-04-2002, 11:54 AM
  4. HOW CAN I CREATE ARRAY IN SQL 6.5
    By Toms Ng in forum Database
    Replies: 1
    Last Post: 12-18-2001, 10:08 AM
  5. Beef up that search engine!
    By Larry Serflaten in forum Talk to the Editors
    Replies: 3
    Last Post: 07-16-2001, 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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