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!! :confused:
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!! :confused:
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!! :WAVE: