Searching an array - output problems - Please help!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Searching an array - output problems - Please help!!

  1. #1
    Join Date
    Nov 2005
    Posts
    44

    Searching an array - output problems - Please help!!

    Hi,

    I'm still plugging away with all these arrays and so forth, but I'm mow trying to search through an array. The array is sorted using a Bubble Sort Algorithm, and I've written a main class to test the searching capabilities.

    The problem is that the output, when I run the main class, is simply freezing and not giving any data. It does not even request a number to input.

    A bit perplexed at the moment!

    Anyway, my code is as follows:

    Code:
    1. The search method from the BubbleSort class:
     
    public int search(int givenNumber)
        {
            for (int i=0; i <numbers.length; i++)
            {
                if (numbers[i] == givenNumber)
                    return i;
            }
            return -1;
            }
     
    2. The main class for testing the search method:
     
    import simplejava.*;
    import week7.testing.*;
     
    public class NumberTester 
    {
        
        
        public static void main(String[] args)
        {
            SimpleWriter screen = new SimpleWriter(System.out);
            SimpleReader keyboard = new SimpleReader(System.in);
            
            BubbleSort internal = new BubbleSort("tester.txt");
            
            boolean done = false;
            while (!done)
            {
                System.out.print("Enter number to search for, -1 to quit: ");
                int givenNumber = keyboard.readInt();
                if (givenNumber == -1)
                    done = true;
                else
                {
                    int pos = internal.search(givenNumber);
                    System.out.println("Found number in position " + pos);
                }
            }
        }
        
    }
    It's probably something really simple, but I just can't see it at the moment.

    Any help really appreciated!!

  2. #2
    Join Date
    Oct 2005
    Posts
    107
    BubbleSort constructor could be throwing an exception??

    FileNotFound maybe.

    put a try catch block around it all and output the exception.

  3. #3
    Join Date
    Nov 2005
    Posts
    44

    Re: Searching an array - output problems - Please help!!

    Hi,

    Thanks for the reply. I've run a similar test main class, just to check that the BubbleSort algorithm does order the array correctly, and there is no exception.

    The output from this main class just freezes at 'run-single:' in the output window, and does not even request the user to input a number to find.

    It's all a bit strange at the moment.

    Here is the code for the BubbleSort class if it helps:

    Code:
    import simplejava.SimpleReader;
    
    public class BubbleSort {
        
    private int [] numbers;
    private int arraySize, size;
        
        public BubbleSort() 
        {
            int[] numbers = new int[10000];
            arraySize = numbers.length;
            size = 0;
        }
        
        public BubbleSort(String fileName)
        {   SimpleReader file = new SimpleReader(fileName);
            int size =0;
            int next = file.readInt();
            while (!file.finished())
            { size++;
              next = file.readInt();
            }
            
            file = new SimpleReader(fileName);
            numbers = new int[size];
            arraySize = size;
            for (int i = 0; i<size; i++)
                numbers[i] = file.readInt();
        }
        
        public void add() //this is a version of the standard Bubble Sort algorithm//
        {
          int out, in;
    
        for (out = numbers.length - 1; out > 1; out--)
          // outer loop (backward)
          for (in = 0; in < out; in++)
            // inner loop (forward)
            if (numbers[in] > numbers[in + 1]) // out of order?
              swap(in, in + 1); // swap them
        }
        
        private void swap(int one, int two) {
        int temp = numbers[one];
        numbers[one] = numbers[two];
        numbers[two] = temp;
      }
        
        public int search(int givenNumber)
        {
            for (int i=0; i <numbers.length; i++)
            {
                if (numbers[i] == givenNumber)
                    return i;
            }
            return -1;
            }
        
        public boolean contains( int n ) 
        {
         for( int i = 0; i < numbers.length; i++ )
              if( numbers[ i ] == n )
                   return true;
         return false;
        }
        
        public String toString()
    {
         String can = "";
          for(int i=0; i<numbers.length; i++)
         {
              can += numbers[i] + "\n";
         }
     
         return can;
    }
    }
    Any help really appreciated!!

  4. #4
    Join Date
    Nov 2005
    Posts
    44

    Re: Searching an array - output problems - Please help!!

    Hi,

    I cannot believe how simple the answer actually was in the end.

    Within the tester main class, all I had to do was add 'ln' to 'System.out.print'.

    It now works perfectly.

    Thanks for all the help!!

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Can you explain WHY you needed the "println"? Was there some other command you could have used to accomplish the same end? The reason is one of those things we need to think about when we are using streams.

Similar Threads

  1. Replies: 1
    Last Post: 11-12-2005, 08:30 PM
  2. Replies: 1
    Last Post: 11-12-2005, 07:15 PM
  3. Searching an array of Objects
    By Philosopher in forum Java
    Replies: 1
    Last Post: 04-16-2005, 03:18 PM
  4. Replies: 2
    Last Post: 04-15-2005, 10:06 PM
  5. Replies: 1
    Last Post: 08-18-2000, 12:29 PM

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