Janine McDaniel Janine McDaniel - 6 months ago 13
Java Question

Java Search array for number

I have a project where I need to search an array of 50 elements and not only print those elements, which is figured out, but I need to find a number, boolean check = false, say 10 and if I do print a message box that I found it!

import java.util.*;

public class IT145_Homework_5_4 {

public static void main(String args[]) {
double alpha[] = new double[50];
boolean check = false;

// Initialize the first 25 elements of the array (int i=0; i<25; i++)
for (int i = 0; i < 25; i++) {
alpha[i] = i * i;
}

// Initialize the last 25 elements of the array (i=25; i<50; i++)
for (int i = 25; i < 50; i++) {
alpha[i] = 3 * i;
}



// Print the element of the array
System.out.println("The values are: ");

print(alpha);

}

// Print method to display the element of the array
private static void print(double m_array[]) {
for (int i = 1; i <= m_array.length; i++) {
System.out.print(m_array[i - 1] + " ");

if (i % 10 == 0)
System.out.print("\n");

}


}
}

Answer
private static void print(double m_array[]) {
    boolean check = false;
    int result = 0;

    for (int i = 1; i <= m_array.length; i++) {
        System.out.print(m_array[i - 1] + " ");

        if (i % 10 == 0)
            System.out.print("\n");

        if(m_array[i-1] == 10){
            result = m_array[i - 1];
            check = true;
        }
    }

    if(check){
    //Print message box with value stored in "result" variable
    }
}