Miranda Miranda - 3 months ago 8
Java Question

How to return a single print statement from a search() method in an array?

I'm having trouble with my search method. What I want to do is have my search method print the statement only once. So if my array contains "3" more than once, I only want to print "3 was found." once instead of checking each value and reporting that there is or is not a "3" at that point in the array. How would I do that?

To clarify, this is what I have:

`0,0,0,0,0,0,0,0,0,0
4,9,6,9,0,8,5,2,8,3
Average Value: 5.4
Maximum Value: 9
Minimum Value: 0
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was found.
2 was not found.
2 was not found.
2 was not found.
2 was not found.
2 was not found.
2 was not found.
2 was not found.
2 was found.
2 was not found.
2 was not found.`


And this is what I want:

0,0,0,0,0,0,0,0,0,0

4,9,6,9,0,8,5,2,8,3

Average Value: 5.4
Maximum Value: 9
Minimum Value: 0
3 was found.
2 was not found.


So this is my complete class. I created a method called initialize that will assign each element in my array a random integer between 0 and 10; a method called print to print out the contents of my array; a method called printStats to find and then print the average, maximum, and minimum value in my array; and a method called search that searches my array (and prints the result) for an integer parameter passed to my method.

Everything works correctly.

public class ArrayLab
{
private int[] array;
public ArrayLab(int numElements)
{
array = new int[numElements];
}
public void initialize()
{
array[0] = (int) (Math.random()*11);
array[1] = (int) (Math.random()*11);
array[2] = (int) (Math.random()*11);
array[3] = (int) (Math.random()*11);
array[4] = (int) (Math.random()*11);
array[5] = (int) (Math.random()*11);
array[6] = (int) (Math.random()*11);
array[7] = (int) (Math.random()*11);
array[8] = (int) (Math.random()*11);
array[9] = (int) (Math.random()*11);
}
public void print() {
System.out.println(array[0] + "," + array[1] + "," + array[2] + "," + array[3] + "," + array[4] + "," + array[5] + "," + array[6] + "," + array[7] + "," + array[8] + "," + array[9]);
System.out.println();
}

public void printStats()
{
double sum = 0;
int max = 0;
int min = 0;
min = array[0];

for (int i = 0; i < array.length; i++)
{
sum = sum + array[i];

if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}

double average = sum/array.length;
System.out.println("Average Value: " + average);
System.out.println("Maximum Value: " + max);
System.out.println("Minimum Value: " + min);


}

public void search(int numChosen)
{
for(int i = 0; i < array.length; i++)
{
if(array[i] == numChosen)
{
System.out.println(numChosen + " was found.");
}
else
{
System.out.println(numChosen + " was not found.");
}
}
}
}

Answer

Start using return or break statement to break the loop after you hit the first successful search.

Also, you should not print the Was Not Found every time while iterating the array. You should print it only once in the end when your array gets exhausted completely and search query is not found.

Here is the modified code snippet:

boolean flag = false;
for(int i = 0; i < array.length; i++)
{
    if(array[i] == numChosen)
    {
        System.out.println(numChosen + " was found.");
        flag = true;
        break;
    }
}

if(!flag) {
    System.out.println(numChosen + " was not found.");
}

Alternatively, you can also do the following:

for(int i = 0; i < array.length; i++)
{
    if(array[i] == numChosen)
    {
        System.out.println(numChosen + " was found.");
        return;
    }
}
System.out.println(numChosen + " was not found.");